如何获取一个元素,其中我 select 它可以在 Selenium Webdriver C# 中更改的属性?

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

我想获取某些元素的 src 属性,如下所示:

    <div class="product-tile restored" data-v-27bc931c=""><a href="/p/apple-iphone-11-pro/R1NFM0000000T7"
        class="product-tile__link">
        <div class="product-tile__image product-img-front">
            <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 class="image-loading"></div>
            <!---->
        </div>
        <div class="product-tile__description">
            <div title=" iPhone 11 Pro" class="product-tile__name"><span class="product-tile__brand">Apple</span> iPhone
                11 Pro
            </div>
            <!---->
        </div>
        <div class="product-tile__footer">
            <div class="product-tile__condition">
                <div>Various conditions<span class="colors"><span class="dot">•</span>4 Colors</span></div>
            </div>
            <div class="product-tile__price">
                <div class="price" data-v-678b2a80="">
                    <div class="current-price" data-v-678b2a80="">
                        <div class="from" data-v-678b2a80="">from</div>
                        <div data-testid="productPrice" class="amount" data-v-678b2a80="">
                            7.00
                        </div>
                    </div>
                    <div data-v-678b2a80=""><span class="amount amount--del" data-v-678b2a80="">49.00</span> <span
                            class="amount amount--save" data-v-678b2a80="">Save
                            2.00</span></div>
                </div>
            </div>
        </div>
        <div class="product-tile__loading"></div>
    </a>
    <!---->
</div>

我正在使用 driver.FindElement(By.CssSelector("img[lazy='loaded")),但问题是这个属性也可以是 loading。当我尝试 driver.FindElement(By.TagName("img")) 时,它不起作用。

我不想使用 TryCatch。我怎样才能得到src?这是我要抓取的website

考虑这两种情况,当 lazy 属性的值为 loadedloading,您可以为 and use the following :

使用逗号分隔值
driver.FindElement(By.CssSelector("img[lazy='loaded'], img[lazy='loading']"))

参考资料

您可以在以下位置找到一些相关的详细讨论: