Image 查找缺少的图像

Image 查找缺少的图像,image,selenium,webdriver,brokenimage,Image,Selenium,Webdriver,Brokenimage,我需要自动(使用selenium)查找网页上丢失的图像。现在可以通过三种方式加载图像: (一) 现在,有了这些在html页面上加载图像的不同方法,我如何编写自动化程序来识别图像是否丢失 问题1)可以使用 List<WebElement> imagesList = _driver.findElements(By.tagName("img")); for (WebElement image : imagesList) { HttpResponse response = new D

我需要自动(使用selenium)查找网页上丢失的图像。现在可以通过三种方式加载图像: (一)

现在,有了这些在html页面上加载图像的不同方法,我如何编写自动化程序来识别图像是否丢失

问题1)可以使用

List<WebElement> imagesList = _driver.findElements(By.tagName("img"));
for (WebElement image : imagesList)
{
    HttpResponse response = new DefaultHttpClient().execute(new HttpGet(image.getAttribute("src");));
    if (response.getStatusLine().getStatusCode() != 200)
    // Do whatever you want with broken images
}
List imagesList=\u driver.findElements(按.tagName(“img”));
用于(WebElement图像:imagesList)
{
HttpResponse response=new DefaultHttpClient().execute(new-HttpGet(image.getAttribute(“src”));
如果(response.getStatusLine().getStatusCode()!=200)
//用破碎的图像做任何你想做的事
}
2)和3)怎么样

谢谢,
Mateen在第二种情况下,src的值将解析为完整URL,因此您可以使用与第一种情况相同的方法

对于第三种情况,您需要通过调用
getCssValue
来获取
背景图像的值。但是,返回值的形式为
url(“http:///assets/images/some_icon.png“”
因此您需要对其进行分析:

WebElement div = driver.findElement(By.className("someIcon"));
String bgImage = div.getCssValue("background-image");

Matcher m = Pattern.compile("\\\".*\\\"").matcher(bgImage);

if (m.find()) {
    String imgUrl = m.group().replace('"', ' ').trim();
    // verify imgUrl
}

第二种情况下,src的值将解析为完整url,您是正确的。谢谢
<div class="someIcon" autoID="some-icon"></div>
.someIcon {
height: 97px;
width: 93px;
background-image: url("../assets/images/some_icon.png");
background-size: 93px 97px;
}
List<WebElement> imagesList = _driver.findElements(By.tagName("img"));
for (WebElement image : imagesList)
{
    HttpResponse response = new DefaultHttpClient().execute(new HttpGet(image.getAttribute("src");));
    if (response.getStatusLine().getStatusCode() != 200)
    // Do whatever you want with broken images
}
WebElement div = driver.findElement(By.className("someIcon"));
String bgImage = div.getCssValue("background-image");

Matcher m = Pattern.compile("\\\".*\\\"").matcher(bgImage);

if (m.find()) {
    String imgUrl = m.group().replace('"', ' ').trim();
    // verify imgUrl
}