The Selenium Browser Automation Project | Selenium
Explicit and Implicit Waits
Waiting is having the automated task execution elapse a certain amount of time before continuing with the next step. You should choose to use Explicit Waits or Implicit Waits.
WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.
Explicit Waits 显式等待
An explicit /ɪkˈsplɪsɪt明确的;清楚的;/ wait is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.
WebDriver driver = new FirefoxDriver(); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return value for the ExpectedCondition function type is a Boolean value of true, or a non-null object.
This example is also functionally equivalent to the first Implicit Waits example.
Expected Conditions
There are some common conditions that are frequently encountered when automating web browsers. Listed below are a few examples for the usage of such conditions. The Java and Python bindings include convenience methods so you don’t have to code an ExpectedCondition class yourself or create your own utility package for them.
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
in
The ExpectedConditions package (Java) (Python) (.NET) contains a set of predefined conditions to use with WebDriverWait.
显式等待,就是明确的要等到某个元素的出现或者是某个元素的可点击等条件,等不到,就一直等,除非在规定的时间之内都没找到,那么就跳出Exception.
如:
new WebDriverWait(driver, 15).until(
ExpectedConditions.presenceOfElementLocated(By.cssSelector("css locator"))
);
这里,15是要等待的秒数.如果没有满足until()方法中的条件,就会始终在这里wait 15秒,依然找不到,就抛出异常.
也可以这样写:
WebDriver driver = new FirefoxDriver();
driver.get( www.baidu.com);
WebElement e = (new WebDriverWait( driver, 10)) .until(
new ExpectedCondition< WebElement>(){
@Override
public WebElement apply( WebDriver d) {
return d.findElement( By.id("id locator"));
}
}
);
这样就通过回调函数,直接获得了这个WebElement.也就是页面元素.
如果只是仅仅想判断页面是不是加载到某个地方了,就可以用第一种方法; 但如果需要得到某个WebElement,两种方式都可以,只是第一种方式还需要再多一步获取的操作.
Wait w = new Wait(){
@Override
public boolean until() {
return webElement.isDisplayed();
}
};
另外这种等待的方式,在加载js代码的时候做判断会比较方便,反正我只在代码里这么见过,但是没用过,有兴趣的朋友可以研究一下.
Implicit Waits 隐式等待
An implicit /ɪmˈplɪsɪt隐式;含蓄的/ wait is to tell WebDriver to poll /pəʊl 投票;轮询/ the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
https://blog.csdn.net/pf20050904/article/details/20052485#commentBox
最近在项目过程中使用selenium 判断元素是否存在的时候 遇到一个很坑爹的问题, 用以下方法执行的时候每次都会等待很长一段时间,原因是因为对selenium实现方法了解不足导致一直找不到解决方法。
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
不明所以, 经过追踪源码才发现启动Driver的时候 使用driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 此处设置的等待时间 是针对全局设置的,webdriver中执行所有命令的超时时间都设置为30秒了, 如上面的findElement方法,找不到元素会默认等待三十秒。 有些时候只需简单判断元素是否存在,立马执行,而此处的设置导致脚本执行缓慢。
driver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS);
隐式等待, 此处的隐式等待是针对Driver 每次执行命令的最长执行时间也可以理解为超时时间,一些人对此处有误解,认为是让Driver等一段时间, 确实某些时候能让Driver等一段时间, 但是影响是全局的,每次Driver执行找不到元素都会等待此处设置的时间,假设某处将此值设置的太长,必须在执行完成之后还原回来,否则判断一个元素是否存在的时候,就会遇到很坑爹的问题。 改进后的方法如下:
WebDriver会进行一个隐式等待,但参数只有时间,这就导致我需要什么元素出现,我不一定能等到它,
private boolean isElementPresent(By by) {
try {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
得不到某个元素,我们就延迟一下...