在Selenium中获取图像src

时间:2017-12-24 11:29:24

标签: java html image selenium

我问过这个问题,但又需要帮助。  我试图在页面中获取所有图像src,以检查页面中是否没有加载图像。 我使用了这些代码但它需要所有图像src' s。如何在" butik-large-image" 类中仅使用这些srcs。

以下是我的代码:

List<WebElement> srcClass;
        String [] cats={"TÜM BUTİKLER","KADIN","ERKEK","ÇOCUK","SPOR GİYİM","AYAKKABI & ÇANTA","SAAT & "
                + "AKSESUAR","KOZMETİK","EV & YAŞAM","HIZLI TESLİMAT"}; 
        for (String kat : cats) {
            sleep(1000);
            clickObjectByLinkText(kat);
            srcClass = driver.findElements(By.tagName("img"));
               System.out.println(kat +" için image src linkleri");
               for (WebElement src : srcClass){
                   if(src.getAttribute("src").equals(null))
                       System.out.println("Ekranda "+kat+" " + " kategorisi için image yüklenemeyen data bulundu,lütfen aşagıdaki pathleri inceleyiniz..");
                   System.out.println(src.getAttribute("src"));
               }
               System.out.println("---------------------------------------------------------------------------------------");
               srcClass.clear();
               }

我的html来源:

<div class="butik-large-image">
                <a href="/Olgun-Orkun-x-Deniz-Akkaya-nin-Sectikleriyle---Kadin-Tekstil/ButikDetay/174246/Kadin" style="font-size: 0" 
title="Olgun Orkun x Deniz Akkaya'nın Seçtikleriyle - Kadın Tekstil" 
class="butik-img-size">
                    <img class="bigBoutiqueImage lazy-load-trigger loaded" 
**src="https://img-trendyol.mncdn.com//Assets/ProductImages/OA/CampaignVisual/OriginalBoutiqueImages/13520/llardau3_0_new.jpg**" data-original="https://img-trendyol.mncdn.com//Assets/ProductImages/OA/CampaignVisual/OriginalBoutiqueImages/13520/llardau3_0_new.jpg" title="Olgun Orkun x Deniz Akkaya'nın Seçtikleriyle - Kadın Tekstil" onerror="this.src='/Resources/images/bigBoutiquePlaceHolder.png'" alt="Olgun Orkun x Deniz Akkaya'nın Seçtikleriyle - Kadın Tekstil">
                    <div class="efect">
</div>
                </a>
</div>

2 个答案:

答案 0 :(得分:1)

想法是:

  • 按className“butik-large-image”
  • 获取所有元素
  • 获取所有具有img标签的元素
  • 编写您想要的代码ex:获取src图像链接,img退出与否等。

您所需要求的完整实施如下:

 List<WebElement> allImagesUnderThisClass = driver.findElements(By.className("butik-large-image"));
    String ignoreImageURl = "https://www.trendyol.com/Resources/images/bigBoutiquePlaceHolder.png";
    for(WebElement elem : allImagesUnderThisClass){
        List<WebElement> imgs = elem.findElements(By.tagName("img"));
        for(WebElement img : imgs){
            String source = img.getAttribute("src");
            if(!source.equalsIgnoreCase(ignoreImageURl)){
                System.out.println(source); 
            }
        }   
    }

答案 1 :(得分:0)

您可以在<img>

的元素下查找butik-large-image标记
srcClass = driver.findElements(By.cssSelector(".butik-large-image img"));

类和标记之间的空格意味着任何后代。