Wait for page load in Selenium

2018-12-31 15:29发布

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

30条回答
美炸的是我
2楼-- · 2018-12-31 15:56

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.

查看更多
十年一品温如言
3楼-- · 2018-12-31 15:57

use a if condition and for any of the element present

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
查看更多
伤终究还是伤i
4楼-- · 2018-12-31 15:59

Ruby implementation:

wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.until {
    @driver.execute_script("return document.readyState;") == "complete" 
}
查看更多
怪性笑人.
5楼-- · 2018-12-31 15:59

You can also use the class: ExpectedConditions to explicitly wait for an element to show up on the webpage before you can take any action further actions

You can use the ExpectedConditions class to determine if an element is visible:

WebElement element = (new WebDriverWait(getDriver(), 10)).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input#houseName")));

See ExpectedConditions class Javadoc for list of all conditions you are able to check.

查看更多
无与为乐者.
6楼-- · 2018-12-31 15:59

SeleniumWaiter:

import com.google.common.base.Function;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SeleniumWaiter {

      private WebDriver driver;

      public SeleniumWaiter(WebDriver driver) {
           this.driver = driver;
      }

      public WebElement waitForMe(By locatorname, int timeout){
           WebDriverWait wait = new WebDriverWait(driver, timeout);
           return wait.until(SeleniumWaiter.presenceOfElementLocated(locatorname));
      }

      public static Function<WebDriver, WebElement> presenceOfElementLocated(final By locator) {
            // TODO Auto-generated method stub
            return new Function<WebDriver, WebElement>() {
                 @Override
                 public WebElement apply(WebDriver driver) {
                      return driver.findElement(locator);
                 }
            };
      }
 }

And to you use it:

_waiter = new SeleniumWaiter(_driver);

try {
   _waiter.waitForMe(By.xpath("//..."), 10);
} 
catch (Exception e) {
   // Error
}
查看更多
查无此人
7楼-- · 2018-12-31 15:59

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.

查看更多
登录 后发表回答