Home > Blockchain >  How to get an element where the attribute by which I select it can change in Selenium Webdriver C#?
How to get an element where the attribute by which I select it can change in Selenium Webdriver C#?

Time:02-14

I want to get the src attribute of some elements like this:

    <div  data-v-27bc931c=""><a href="/p/apple-iphone-11-pro/R1NFM0000000T7"
        >
        <div >
            <img alt="Apple  iPhone 11 Pro"
                data-src="/img?width=300&amp;height=300&amp;action=resize&amp;url=/catalog/product/6/f/6f7466aef1ae14ee4fe9764531ef69a1.jpg"
                src="/img?width=300&amp;height=300&amp;action=resize&amp;url=/catalog/product/6/f/6f7466aef1ae14ee4fe9764531ef69a1.jpg"
                lazy="loaded">
            <div ></div>
            <!---->
        </div>
        <div >
            <div title=" iPhone 11 Pro" ><span >Apple</span> iPhone
                11 Pro
            </div>
            <!---->
        </div>
        <div >
            <div >
                <div>Various conditions<span ><span >•</span>4 Colors</span></div>
            </div>
            <div >
                <div  data-v-678b2a80="">
                    <div  data-v-678b2a80="">
                        <div  data-v-678b2a80="">from</div>
                        <div data-testid="productPrice"  data-v-678b2a80="">
                            $647.00
                        </div>
                    </div>
                    <div data-v-678b2a80=""><span  data-v-678b2a80="">$1049.00</span> <span
                             data-v-678b2a80="">Save
                            $402.00</span></div>
                </div>
            </div>
        </div>
        <div ></div>
    </a>
    <!---->
</div>

I'm using driver.FindElement(By.CssSelector("img[lazy='loaded")) but the problem is that this attribute can also be loading. When I try driver.FindElement(By.TagName("img")) that doesn't work.

I don't want to use Try and Catch. How to can I get the src? Here is the website that i want to scrape

CodePudding user response:

To consider both the cases, when value of lazy attribute is loaded and loading, you can use comma seperated values for the and use the following locator strategy:

driver.FindElement(By.CssSelector("img[lazy='loaded'], img[lazy='loading']"))

References

You can find a couple of relevant detailed discussions in:

  • Related