【问题标题】:How to verify dynamic gallery with Selenium WebDriver using Java?如何使用 Java 使用 Selenium WebDriver 验证动态图库?
【发布时间】:2017-11-03 20:18:34
【问题描述】:

问题:

图库中的图像始终是动态的,但文章不断变化。当我点击文章时,图片库会打开一个大图片和其他带有轮播的缩略图。


第一个场景:

我需要验证图像是否已加载并且图库是否正常。其他缩略图图像也必须进行导航和验证。`

示例:

    1. 我在文章页面上并打开了该文章,因此该文章页面打开了一个画廊,其中包含 > 5 个缩略图图像。
    1. 现在我需要验证图像是否已打开,并且还要逐个导航以验证缩略图。

第二个场景:

有时这篇文章会加载图像,有时它可以有视频或只有一个图像。所以这就是我的意思是动态的。`

所以每次图像变化时,需要知道如何处理这种情况 下面是html代码:

<div class="carousel royalSlider rsDefault rsAutoHeight rsHor  rsWithThumbs rsWithThumbsHor" style="min-height: 463.8px;">
    <div class="rsOverflow" style="width: 773px; height: 436px; transition:  height 600ms ease-in-out;">
        <div class="rsContainer" style="transition-duration: 600ms; transform:  translate3d(-797px, 0px, 0px); transition-timing-function: cubic-bezier(0.445,  0.05, 0.55, 0.95);">
            <div style="left: 0px;" class="rsSlide"><img class="rsImg  rsMainSlideImage"  src="http://resources.carsguide.com.au/styles/cg_hero_large/s3/lexus-rx350-2016- (1).jpg"></div>
            <div style="left: 797px;" class="rsSlide rsActiveSlide"><img  class="rsImg rsMainSlideImage"  src="http://resources.carsguide.com.au/styles/cg_hero_large/s3/lexus-rx350-2016- (2).jpg"></div>
            <div style="left: 1594px;" class="rsSlide "><img class="rsImg  rsMainSlideImage"  src="http://resources.carsguide.com.au/styles/cg_hero_large/s3/lexus-rx350-2016- (3).jpg"></div>
            <div style="left: 2391px;" class="rsSlide "><img class="rsImg  rsMainSlideImage"  src="http://resources.carsguide.com.au/styles/cg_hero_large/s3/lexus-rx350-2016- (4).jpg"></div>
        </div>
        <div class="rsFullscreenBtn">
            <div class="rsFullscreenIcn"></div>
        </div>
        <div class="rsArrow rsArrowLeft" style="display: block;">
            <div class="rsArrowIcn"></div>
        </div>
        <div class="rsArrow rsArrowRight" style="display: block;">
            <div class="rsArrowIcn"></div>
        </div>
        <div class="rsGCaption">Lexus RX 350 F Sport.<noscript>&lt;img  src=http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(2).jpg&gt;</noscript></div>
    </div>
    <div class="rsNav rsThumbs rsThumbsHor">
        <div class="rsThumbsContainer" style="transition-property: -webkit- transform; transform: translate3d(86.5px, 0px, 0px); width: 612px;">
            <div style="margin-right:12px;" class="rsNavItem rsThumb"><img  class="rsTmb lazyload" onerror="this.style.visibility='hidden'" data- src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(1).jpg"  src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(1).jpg" width="90" height="50" alt=""><span class="thumbIco"></span></div>
            <div style="margin-right:12px;" class="rsNavItem rsThumb  rsNavSelected"><img class="rsTmb lazyload"  onerror="this.style.visibility='hidden'" data- src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(2).jpg"  src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(2).jpg" width="90" height="50" alt=""><span class="thumbIco"></span></div>
            <div style="margin-right:12px;" class="rsNavItem rsThumb"><img c lass="rsTmb lazyload" onerror="this.style.visibility='hidden'" data- src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(3).jpg"  src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(3).jpg" width="90" height="50" alt=""><span class="thumbIco"></span></div>
            <div style="margin-right:12px;" class="rsNavItem rsThumb"><img  class="rsTmb lazyload" onerror="this.style.visibility='hidden'" data- src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(4).jpg"  src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(4).jpg" width="90" height="50" alt=""><span class="thumbIco"></span></div>
            <div style="margin-right:12px;" class="rsNavItem rsThumb"><img  class="rsTmb lazyload" onerror="this.style.visibility='hidden'" data- src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(5).jpg"  src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(5).jpg" width="90" height="50" alt=""><span class="thumbIco"></span></div>
            <div style="margin-right:12px;" class="rsNavItem rsThumb"><img  class="rsTmb lazyload" onerror="this.style.visibility='hidden'" data- src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(6).jpg"  src="http://resources.carsguide.com.au/styles/cg_hero_thumbnail/s3/lexus-rx350- 2016-(6).jpg" width="90" height="50" alt=""><span class="thumbIco"></span></div>
        </div>
        <div class="rsThumbsArrow rsThumbsArrowLeft  rsThumbsArrowDisabled">
            <div class="rsThumbsArrowIcn"></div>
        </div>
        <div class="rsThumbsArrow rsThumbsArrowRight rsThumbsArrowDisabled">
            <div class="rsThumbsArrowIcn"></div>
        </div>
    </div>
    <div style="clear:both; float: none;"></div>
</div>

到目前为止,我所做的只是:

    1. 打开网址。
    1. 单击文章页面。
    1. 现在打开该/任何文章。

我的第四步是在文章打开时验证该图像(带有缩略图)。我无法做到第四步。我现在没有任何错误,我可以成功完成到第 3 步。

【问题讨论】:

  • 动态图库是什么意思,请具体说明
  • @rajNishKuMar ex:我在一个文章页面上,我打开了那篇文章,所以这个文章页面打开了一个画廊,里面有 > 5 个缩略图。现在我需要验证图像是否已打开,并且还需要一个接一个地导航以验证缩略图。第二个场景是有时这篇文章会加载图像,有时它可以有视频;或者只是一个图像。所以这就是我的意思是动态
  • 好的,在这种情况下,您面临的问题是如何判断哪个时间图像在那里以及哪个时间视频在那里
  • 到目前为止,我所做的只是 1. 打开 URL 2. 单击文章页面 3. 现在打开该/任何文章。我的第四步是在文章打开时验证该图像(带有缩略图)。我无法做到第四步。我现在没有任何错误,我能够成功地做到这一点,直到第 3 步。以下是网站示例:carsguide.com.au/car-reviews/…
  • 谢谢,会试一试告诉你。,怎么样

标签: java html selenium-webdriver


【解决方案1】:

您好,请按照以下方式进行操作

public class WorkingWithThumbNails {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("http://www.carsguide.com.au/car-reviews/toyota-tarago-used-review-1990-2015-41984");

    // Now count the number of images in the thumb-nails cause
    // this will decide number of clicks on the next Arrow

    List<WebElement> myThumbnails = driver.findElements(By.cssSelector(".rsTmb.lazyload"));
    List<WebElement> myMainImages = driver.findElements(By.cssSelector(".rsImg.rsMainSlideImage"));

    System.out.println("Size of the thumbnail is : " + myThumbnails.size());
    System.out.println("Size of the main image is : " + myMainImages.size());


    // print the urls of the thumb-nails as well
    for(int i=0;i< myThumbnails.size();i++){
        myThumbnails = driver.findElements(By.cssSelector(".rsTmb.lazyload"));
        System.out.println("Url for the Thumbnail is : " + myThumbnails.get(i).getAttribute("src"));

        myMainImages = driver.findElements(By.cssSelector(".rsImg.rsMainSlideImage"));
        System.out.println("Url for the main image is : " + myMainImages.get(i).getAttribute("src"));

        // now implement the logic of img verification, logic can be many but here i used if src for bigger img 
        // contains img name as per the thumbnail img name then its ok img is properly loaded and hence it is right image.

        String[] myMainImgName = myMainImages.get(i).getAttribute("src").split("s3");

        if(myThumbnails.get(i).getAttribute("src").contains(myMainImgName[1])){
            System.out.println("Main image is as per the thumbnail.");
            // to verify next thumb-nail img with main img we have to click
            // here if is because loop will run one more time then img size
            if(driver.findElements(By.xpath("//*[@class='rsArrow rsArrowRight']/div")).size() > 0){
                driver.findElement(By.xpath("//*[@class='rsArrow rsArrowRight']/div")).click();
            }
        }else{
            // apply the same logic for videos 
            // hope you can do this 
        }
    }

}

}

如有疑问请随时询问。

【讨论】:

    猜你喜欢
    • 2016-04-09
    • 2019-08-13
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多