简体   繁体   English

如何断言动态图像不存在于 Selenium Python Web 驱动程序的网页中

[英]How to assert that a dynamic image is not present in a webpage in Selenium Python web driver

There is webpage that loads 4 images dynamically.有动态加载 4 张图片的网页。 They cannot be clicked and in the source code there is only src attribute.它们不能被点击,并且在源代码中只有src属性。 Using XPath I found the URL for each image.使用 XPath 我找到了每个图像的 URL。 How do I find if a particular image is present or not on page load?如何确定页面加载时是否存在特定图像? I need this in Selenium Python web driver.我在 Selenium Python Web 驱动程序中需要这个。

Below is the code which I need to automate:下面是我需要自动化的代码:

<div class="row">
<div id="1" class="xx">
    <div class="row">
        <div class="yy"> <img src="/img/images/pic-1.jpg"> </div>
        <div class="a"> hello 1 </div>
    </div>
    <br>
    <br>
    <div class="row">
        <div class="yy"> <img src="/img/images/pic-2.jpg"> </div>
        <div class="a"> Hello 2 </div>
    </div>
    <br>
    <br>
    <div class="row">
        <div class="yy"> <img src="/img/images/pic-3.jpg"> </div>
        <div class="a"> Hello 3 </div>
    </div>
</div>
</div>

The image keeps changing every time the page is loaded.每次加载页面时,图像都会不断变化。 I need to find if pic-1.jpg is present/displayed when the page is loaded.我需要查找页面加载时是否存在/显示pic-1.jpg

I don't know python but since you don't have any other answers I'll try to help you with some Java code that hopefully you can translate.我不知道 python 但由于您没有任何其他答案,我将尝试帮助您处理一些希望您可以翻译的 Java 代码。

You basically want to grab all the IMG tags inside a DIV with class yy .您基本上想要获取类yyDIV所有IMG标签。 You then loop through that collection of elements looking for the desired picture name in the src string.然后循环遍历该元素集合,在src字符串中查找所需的图片名称。 If it is found, the element HTML is printed.如果找到,则打印元素 HTML。

List<WebElement> images = driver.findElements(By.cssSelector("div.yy > img"));
String imageFileName = "pic-1.jpg";
for (WebElement image : images)
{
    if (image.getAttribute("src").contains(imageFileName))
    {
        System.out.println(image.getAttribute("innerHTML"));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM