Wait for page load in Selenium

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

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

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

    Imran's answer rehashed for Java 7:

        WebDriverWait wait = new WebDriverWait(driver, 30);
    
        wait.until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver wdriver) {
                return ((JavascriptExecutor) driver).executeScript(
                    "return document.readyState"
                ).equals("complete");
            }
        });
    
    0 讨论(0)
  • 2020-11-22 07:59

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

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

    this wait for every element on page for 30 sec.

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

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

    In id give static element id which is diffidently display on the page, as soon as page is load.

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

    You can use the below existing method to set the time for pageeLoadTimeout in below example if the page is taking more than 20 seconds to load , then it will throw an exception of page reload

     WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS)
    
    0 讨论(0)
  • 2020-11-22 08:00

    You can explicitly wait for an element to show up on the webpage before you can take any action (like element.click())

    driver.get("http://somedomain/url_that_delays_loading");
    WebElement myDynamicElement = (new WebDriverWait(driver, 10))
      .until(new ExpectedCondition<WebElement>(){
            @Override
            public WebElement apply(WebDriver d) {
            return d.findElement(By.id("myDynamicElement"));
    }});
    

    This is what I used for a similar scenario and it works fine.

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

    You can also check pageloaded using following code

    IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
    
     wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
    
    0 讨论(0)
  • 2020-11-22 08:04

    Use class WebDriverWait

    Also see here

    You can expect to show some element. something like in C#:

    WebDriver _driver = new WebDriver();
    WebDriverWait _wait = new WebDriverWait(_driver, new TimeSpan(0, 1, 0));
    
    _wait.Until(d => d.FindElement(By.Id("Id_Your_UIElement"));
    
    0 讨论(0)
提交回复
热议问题