Wait for page load in Selenium

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

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

相关标签:
30条回答
  • 2020-11-22 07:53

    In general, with Selenium 2.0 the web driver should only return control to the calling code once it has determined that the page has loaded. If it does not, you can call waitforelemement, which cycles round calling findelement until it is found or times out (time out can be set).

    0 讨论(0)
  • 2020-11-22 07:53

    Ruby implementation:

    wait = Selenium::WebDriver::Wait.new(:timeout => 10)
    wait.until {
        @driver.execute_script("return document.readyState;") == "complete" 
    }
    
    0 讨论(0)
  • 2020-11-22 07:54

    Call below Function in your script , this will wait till page is not loaded using javascript

    public static boolean isloadComplete(WebDriver driver)
    {
        return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("loaded")
                || ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
    }
    
    0 讨论(0)
  • 2020-11-22 07:55

    Explicitly wait or conditional wait in this wait until given this condition.

    WebDriverWait wait = new WebDriverWait(wb, 60);
    wait.until(ExpectedConditions.elementToBeClickable(By.name("value")));
    

    This will wait for every web element for 60 seconds.

    Use implicitly wait for wait of every element on page till that given time.

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

    This will wait for every web element for 60 seconds.

    0 讨论(0)
  • 2020-11-22 07:56
    /**
     * Call this method before an event that will change the page.
     */
    private void beforePageLoad() {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.mpPageReloaded='notYet';");
    }
    
    /**
     * Call this method after an event that will change the page.
     * 
     * @see #beforePageLoad
     * 
     *      Waits for the previous page to disappear.
     */
    private void afterPageLoad() throws Exception {
        (new WebDriverWait(driver, 10)).until(new Predicate<WebDriver>() {
    
            @Override
            public boolean apply(WebDriver driver) {
                JavascriptExecutor js = (JavascriptExecutor) driver;
                Object obj = js.executeScript("return document.mpPageReloaded;");
                if (obj == null) {
                    return true;
                }
                String str = (String) obj;
                if (!str.equals("notYet")) {
                    return true;
                }
                return false;
            }
        });
    }
    

    You can change from the document to an element, in the case of where only part of a document is being changed.

    This technique was inspired by the answer from sincebasic.

    0 讨论(0)
  • 2020-11-22 07:58

    All of these solutions are OK for specific cases, but they suffer from at least one of a couple of possible problems:

    1. They are not generic enough -- they want you to know, ahead of time, that some specific condition will be true of the page you are going to (eg some element will be displayed)

    2. They are open to a race condition where you use an element that is actually present on the old page as well as the new page.

    Here's my attempt at a generic solution that avoids this problem (in Python):

    First, a generic "wait" function (use a WebDriverWait if you like, I find them ugly):

    def wait_for(condition_function):
        start_time = time.time()
        while time.time() < start_time + 3:
            if condition_function():
                return True
            else:
                time.sleep(0.1)
        raise Exception('Timeout waiting for {}'.format(condition_function.__name__))
    

    Next, the solution relies on the fact that selenium records an (internal) id-number for all elements on a page, including the top-level <html> element. When a page refreshes or loads, it gets a new html element with a new ID.

    So, assuming you want to click on a link with text "my link" for example:

    old_page = browser.find_element_by_tag_name('html')
    
    browser.find_element_by_link_text('my link').click()
    
    def page_has_loaded():
        new_page = browser.find_element_by_tag_name('html')
        return new_page.id != old_page.id
    
    wait_for(page_has_loaded)
    

    For more Pythonic, reusable, generic helper, you can make a context manager:

    from contextlib import contextmanager
    
    @contextmanager
    def wait_for_page_load(browser):
        old_page = browser.find_element_by_tag_name('html')
    
        yield
    
        def page_has_loaded():
            new_page = browser.find_element_by_tag_name('html')
            return new_page.id != old_page.id
    
        wait_for(page_has_loaded)
    

    And then you can use it on pretty much any selenium interaction:

    with wait_for_page_load(browser):
        browser.find_element_by_link_text('my link').click()
    

    I reckon that's bulletproof! What do you think?

    More info in a blog post about it here

    0 讨论(0)
提交回复
热议问题