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

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

  • 有一个名为’Products’的页面。 当我点击“产品”页面中的“查看详细信息”链接时,会出现一个包含该项目详细信息的弹出窗口(modal dialog)。
  • 当我点击弹出窗口中的“关闭”按钮时,弹出窗口关闭,页面自动刷新(页面刚刚重新加载,内容保持不变)。
  • 关闭弹出窗口后,我需要点击同一页面中的“添加项目”按钮。 但是当webdriver试图找到“添加项目”按钮时,如果网速太快,webdriver就可以找到并单击该元素。

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

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

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

3个答案,你可以结合:

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

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

C#

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

Java的

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

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

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

 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(); 

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

 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

是(当你的方案)你已经定义了定位器策略以首先单击“添加项目”然后当你关闭弹出窗口时刷新页面得到刷新因此为“添加项目”定义的引用在记忆所以要克服这个问题,你必须再次重新定义“添加项目”的定位器策略

用虚拟代码理解它

 // 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 

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

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

您可以使用的第二个就是使用该方法尝试catch方法,您可以获得您想要的结果。如果您希望示例代码随意与我联系,我将提供相关代码