Selenium - 如何等待页面完全加载

时间:2016-04-13 06:27:07

标签: java selenium-webdriver

我正在尝试使用Java和Selenium WebDriver自动化一些测试用例。我有以下情况:

  • 有一个名为' Products'的页面。当我点击'查看详情'链接 在'产品'页面,将显示包含该项目详细信息的弹出窗口(模态对话框)。
  • 当我点击'关闭'弹出窗口关闭弹出窗口中的按钮 页面自动刷新(页面只是重新加载,内容保持不变)。
  • 关闭弹出窗口后,我需要点击'添加项目'按钮 同一页。但是当WebDriver试图找到“添加项目”时按钮, 如果互联网速度太快,WebDriver可以找到并点击 元件。

  • 但如果互联网速度慢,WebDriver会在之前找到按钮 页面刷新,但只要WebDriver单击该按钮,页面就会刷新并发生StaleElementReferenceException

  • 即使使用了不同的等待,所有等待条件也都成立 (因为重新加载前后页面中的内容相同) 甚至在重新加载页面之前StaleElementReferenceException 发生。

如果在点击“添加项目”之前使用Thread.sleep(3000);,则测试用例可以正常工作。按钮。这个问题还有其他解决方法吗?

5 个答案:

答案 0 :(得分:55)

3个答案,您可以将它们结合起来:

1。)创建Web驱动程序实例后立即设置隐式等待:driver.manage().timeouts().implicitlyWait()。这将尝试等到页面在每个页面导航或页面重新加载时完全加载。

2.。)页面导航后,调用JavaScript return document.readyState,直到返回"complete"。 Web驱动程序实例可以充当JavaScript执行程序。示例代码:

C#

new WebDriverWait(driver, MyDefaultTimeout).Until(
    d => ((IJavaScriptExecutor) d).ExecuteScript("return document.readyState").Equals("complete"));

爪哇

new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
          webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));

3。)在2.)之后,检查URL是否与您期望的模式匹配。

答案 1 :(得分:12)

在点击"添加"之前,您似乎需要等待重新加载页面。按钮。 在这种情况下,您可以等待"添加项目"在单击重新加载的元素之前要变为陈旧的元素:

WebDriverWait wait = new WebDriverWait(driver, 20);
By addItem = By.xpath("//input[.='Add Item']");

// get the "Add Item" element
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));

//trigger the reaload of the page
driver.findElement(By.id("...")).click();

// wait the element "Add Item" to become stale
wait.until(ExpectedConditions.stalenessOf(element));

// click on "Add Item" once the page is reloaded
wait.until(ExpectedConditions.presenceOfElementLocated(addItem)).click();

答案 2 :(得分:5)

在点击添加项目之前,您可以通过多种方式执行此操作:

WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.elementToBeClickable(By.id("urelementid")));// instead of id u can use cssSelector or xpath of ur element.


or

wait.until(ExpectedConditions.visibilityOfElementLocated("urelement"));
你也可以这样等。如果你想等到上一页元素不可见:

wait.until(ExpectedConditions.invisibilityOfElementLocated("urelement"));

这里是你找到所有可用于等待及其文档的selenium webdriver api的链接。

https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

答案 3 :(得分:-1)

是(当你的场景)你已经定义了定位器策略以首先点击“添加项目”然后当你关闭弹出窗口时刷新页面因此为“添加项目”定义的引用是丢失在记忆中以便克服这个问题,你必须重新定义“添加项目”的定位器策略

用虚拟代码理解它

// clicking on view details 
driver.findElement(By.id("")).click();
// closing the pop up 
driver.findElement(By.id("")).click();


// and when you try to click on Add Item
driver.findElement(By.id("")).click();
// you get stale element exception as reference to add item is lost 
// so to overcome this you have to re identify the locator strategy for add item 
// Please note : this is one of the way to overcome stale element exception 

// Step 1 please add a universal wait in your script like below 
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // just after you have initiated browser

答案 4 :(得分:-4)

有两种不同的方法可以在硒中使用延迟,最常用的是硒。请试试这个:

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

你可以使用的第二个就是使用该方法的try catch方法 如果您想要示例代码随时与我联系,我将提供相关代码

相关问题