2015-08-13 74 views
-1

我试图验证图像是否存在于网页上。你能建议我最可行的代码吗?我在下面给出必要的细节。使用Selenium Webdriver验证网页上的图像存在

(这里我指的是在左上侧的主产品图像中绿色)

页网址:http://www.marksandspencer.com/ditsy-floral-tunic/p/p60072079

另外,如果你想我可以发送屏幕截图。请发给我电子邮件ID。

请尽早提出,因为我需要它。图像将被显示=)

+1

请添加相关的HTML代码和你已经尝试解决你 – drkthng

+0

的这个问题理论问题 - >理论答案:如果sourcecode.contains(“foo.jpg”) - > yolo。 – C4u

回答

0

尝试通过XPath来检查发现元素的大小如果图像是本,

  1. 察看此IMG SRC包含文件名
  2. 察看此IMG webelement大小如果大于第一个你想要的尺寸
  3. 这一个有点超出了webdriver的范围,你需要从img webelement获得src属性,然后向src发出GET请求,看看你是否得到200 OK。

上述验证只会帮助确保页面上存在图像,但除非进行图像比较,否则无法验证所需图像是否正在显示。

如果你想要做的图像进行比较,然后看看https://github.com/facebookarchive/huxley

0

下面是一些方法,使用它可以验证 -

".//*[@class='s7staticimage']/img" 

如果宽度大于10px的:

0

这里是验证网页中的所有图片的代码 - 硒webdriver的,TestNG的,Java的。

公共无效testAllImages(){

//测试网页 - www.yahoo.com

wd.get("https://www.yahoo.com"); 

    //Find total No of images on page and print In console. 
    List<WebElement> total_images = wd.findElements(By.tagName("img")); 
    System.out.println("Total Number of images found on page = " + total_images.size()); 

    //for loop to open all images one by one to check response code. 
    boolean isValid = false; 
    for (int i = 0; i < total_images.size(); i++) { 
    String url = total_images.get(i).getAttribute("src"); 

    if (url != null) { 

     //Call getResponseCode function for each URL to check response code. 
     isValid = getResponseCode(url); 

     //Print message based on value of isValid which Is returned by getResponseCode function. 
     if (isValid) { 
     System.out.println("Valid image:" + url); 
     System.out.println("----------XXXX-----------XXXX----------XXXX-----------XXXX----------"); 
     System.out.println(); 
     } else { 
     System.out.println("Broken image ------> " + url); 
     System.out.println("----------XXXX-----------XXXX----------XXXX-----------XXXX----------"); 
     System.out.println(); 
     } 
    } else {  
     //If <a> tag do not contain href attribute and value then print this message 
     System.out.println("String null"); 
     System.out.println("----------XXXX-----------XXXX----------XXXX-----------XXXX----------"); 
     System.out.println(); 
     continue; 
    } 
    } 
} 
相关问题