使用Selenium Web Driver测试动态加载的内容

Ben*_*rig 6 selenium webdriver gui-testing selenium-webdriver

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

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

我尝试了以下内容向下滚动,直到不再加载内容:

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);
Run Code Online (Sandbox Code Playgroud)

但是,虽然页面第一次向下滚动,而现在一些内容正确加载,但驱动程序的findElementsBy(By)函数找不到它们.

有人曾遇到过这个问题吗?如果有人能帮助我找到解决办法,我会很高兴的!

此致,本杰明

Sta*_*Quo 5

我建议将 WebDriverWait 与 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()
Run Code Online (Sandbox Code Playgroud)

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


pel*_*cid 0

我认为问题在于等待动态内容完成加载。尝试在 findElementsBy 之前等待 3 秒?在 C# 中,代码为 Thread.Sleep(3000);