Wait for page load in Selenium

后端 未结 30 2934
攒了一身酷
攒了一身酷 2020-11-22 07:12

How do you make Selenium 2.0 wait for the page to load?

30条回答
  •  无人共我
    2020-11-22 08:10

    If you want to wait for a specific element to load, you can use the isDisplayed() method on a RenderedWebElement :

    // Sleep until the div we want is visible or 5 seconds is over
    long end = System.currentTimeMillis() + 5000;
    while (System.currentTimeMillis() < end) {
        // Browsers which render content (such as Firefox and IE) return "RenderedWebElements"
        RenderedWebElement resultsDiv = (RenderedWebElement) driver.findElement(By.className("gac_m"));
    
        // If results have been returned, the results are displayed in a drop down.
        if (resultsDiv.isDisplayed()) {
          break;
        }
    }
    

    (Example from The 5 Minute Getting Started Guide)

提交回复
热议问题