使用Selenium Web驱动程序测试动态加载的内容 [英] Test dynamically loaded content with Selenium Web Driver

查看:120
本文介绍了使用Selenium Web驱动程序测试动态加载的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个系统,该系统具有我正在使用Selenium测试的基于Web的前端.在页面上向下滚动时会动态加载内容(也许您从Facebook的朋友列表中知道了该内容),因为这是要求之一.

I am working on a system that has a web based frontend that I am testing with Selenium. On one page the content is dynamically loaded when scrolling down (maybe you know that from Facebook's friend-list), because it is one of the requirements.

使用Selenium Webdriver(我使用Chrome)向下滚动,通过Javascript应该没问题.但是动态添加的内容存在问题.如何使Webdriver查找那些元素?

Scrolling down with Selenium Webdriver (I use Chrome) should be no problem via Javascript. But there is a problem with the dynamically added content. How can I make the Webdriver find those elements?

我尝试了以下操作,直到没有更多内容加载为止:

I tried the following to scroll down until no more content is loaded:

int oldSize = 0;
int newSize = 0;
do {
  driver.executeScript("window.scrollTo(0,document.body.scrollHeight)");
  newSize = driver.findElementsBy(By.cssSelector("selector").size();
} while(newSize > oldSize);

但是,尽管页面第一次向下滚动并且现在已经正确加载了一些内容,但驱动程序的findElementsBy(By)函数将找不到这些内容.

But though the page scrolls down the first time and some now content is loaded correctly, they will not be found by the drivers' findElementsBy(By) function.

有人遇到过这个问题吗?如果有人可以帮助我解决该问题,我将感到非常高兴!

Has someone ever faced this problem?? I'd be very glad if someone could help me figuring a solution for that!

关于本杰明

推荐答案

我建议将WebDriverWait与ExpectedConditons一起使用.

I would recommend using WebDriverWait with ExpectedConditons.

//scroll down with Javascript first
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("selector")));
//interact with your element
element.click()

看看Selenium官方页面提供的指导: http://seleniumhq.org/docs/04_webdriver_advanced.html

Take a look at the guidance provided by Selenium Official page: http://seleniumhq.org/docs/04_webdriver_advanced.html

这篇关于使用Selenium Web驱动程序测试动态加载的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆