Wait for page load in Selenium

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

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

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

    You can use wait. there are basically 2 types of wait in selenium

    • Implicit wait
    • Explicit wait

    - Implicit wait

    This is very simple please see syntax below:

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

    - Explicit wait

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

    WebDriverWait wait = new WebDriverWait(driver, 40);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
    

    You can use other properties like visblityOf(), visblityOfElement()

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

    In my case , I used the following to know the page load status. In our application loading gif(s) are present and, I listen to them as follows to eliminate unwanted wait time in the script.

    public static void processing(){ 
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='Msgpanel']/div/div/img")));
        wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[@id='Msgpanel']/div/div/img")));
    }
    

    Where the xpath locates the gif in the HTML DOM. After this, You may also implement your action methods Click.

    public static void click(WebElement elementToBeClicked){
        WebDriverWait wait = new WebDriverWait(driver, 45);
        wait.until(ExpectedConditions.visibilityOf(element));
        wait.until(ExpectedConditions.elementToBeClickable(element)); 
        wait.ignoring(NoSuchElementException.class).ignoring(StaleElementReferenceException.class); elementToBeClicked.click(); 
     }
    
    0 讨论(0)
  • 2020-11-22 08:06

    How to get Selenium to wait for page load after a click provides the following interesting approach:

    1. Store a reference to a WebElement from the old page.
    2. Click the link.
    3. Keep on invoking operations on the WebElement until StaleElementReferenceException is thrown.

    Sample code:

    WebElement link = ...;
    link.click();
    new WebDriverWait(webDriver, timeout).until((org.openqa.selenium.WebDriver input) ->
    {
        try
        {
            link.isDisplayed();
            return false;
        }
        catch (StaleElementReferenceException unused)
        {
            return true;
        }
    });
    
    0 讨论(0)
  • 2020-11-22 08:07

    If you set the implicit wait of the driver, then call the findElement method on an element you expect to be on the loaded page, the WebDriver will poll for that element until it finds the element or reaches the time out value.

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

    source: implicit-waits

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

    The best way to wait for page loads when using the Java bindings for WebDriver is to use the Page Object design pattern with PageFactory. This allows you to utilize the AjaxElementLocatorFactory which to put it simply acts as a global wait for all of your elements. It has limitations on elements such as drop-boxes or complex javascript transitions but it will drastically reduce the amount of code needed and speed up test times. A good example can be found in this blogpost. Basic understanding of Core Java is assumed.

    http://startingwithseleniumwebdriver.blogspot.ro/2015/02/wait-in-page-factory.html

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

    Here is a Java 8 version of the currently most upvoted answer:

    WebDriverWait wait = new WebDriverWait(myDriver, 15);
    wait.until(webDriver -> ((JavascriptExecutor) myDriver).executeScript("return document.readyState").toString().equals("complete"));
    

    Where myDriver is a WebDriver object (declared earlier).

    Note: Be aware that this method (document.readyState) only checks the DOM.

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