Wait for page load in Selenium

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

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

相关标签:
30条回答
  • 2020-11-22 08:09

    I'm surprised that predicates weren't the first choice as you typically know what element(s) you will next interact with on the page you're waiting to load. My approach has always been to build out predicates/functions like waitForElementByID(String id) and waitForElemetVisibleByClass(String className), etc. and then use and reuse these wherever I need them, be it for a page load or page content change I'm waiting on.

    For example,

    In my test class:

    driverWait.until(textIsPresent("expectedText");
    

    In my test class parent:

    protected Predicate<WebDriver> textIsPresent(String text){
        final String t = text;
        return new Predicate<WebDriver>(){
            public boolean apply(WebDriver driver){
                return isTextPresent(t);
            }
        };
    }
    
    protected boolean isTextPresent(String text){
        return driver.getPageSource().contains(text);
    }
    

    Though this seems like a lot, it takes care of checking repeatedly for you and the interval for how often to check can be set along with the ultimate wait time before timing out. Also, you will reuse such methods.

    In this example, the parent class defined and initiated the WebDriver driver and the WebDriverWait driverWait.

    I hope this helps.

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-11-22 08:10

    The best way I've seen is to utilize the stalenessOf ExpectedCondition, to wait for the old page to become stale.

    Example:

    WebDriver driver = new FirefoxDriver();
    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    WebElement oldHtml = driver.findElement(By.tagName("html"));
    wait.until(ExpectedConditions.stalenessOf(oldHtml));
    

    It'll wait for ten seconds for the old HTML tag to become stale, and then throw an exception if it doesn't happen.

    0 讨论(0)
  • 2020-11-22 08:12

    You may remove the System.out line. It is added for debug purposes.

    WebDriver driver_;
    
    public void waitForPageLoad() {
    
        Wait<WebDriver> wait = new WebDriverWait(driver_, 30);
        wait.until(new Function<WebDriver, Boolean>() {
            public Boolean apply(WebDriver driver) {
                System.out.println("Current Window State       : "
                    + String.valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState")));
                return String
                    .valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState"))
                    .equals("complete");
            }
        });
    }
    
    0 讨论(0)
  • 2020-11-22 08:12

    This seems to be a serious limitation of WebDriver. Obviously waiting for an element will not imply the page being loaded, in particular the DOM can be fully build (onready state) whereby JS is still executing and CSS and images are still loading.

    I believe the simplest solution is to set a JS variable upon the onload event after everything is initialized and check and wait for this JS variable in Selenium.

    0 讨论(0)
  • 2020-11-22 08:13

    I use node + selenium-webdriver(which version is 3.5.0 now). what I do for this is:

    var webdriver = require('selenium-webdriver'),
        driver = new webdriver.Builder().forBrowser('chrome').build();
    ;
    driver.wait(driver.executeScript("return document.readyState").then(state => {
      return state === 'complete';
    }))
    
    0 讨论(0)
提交回复
热议问题