Transfer from Http://blog.csdn.net/pf20050904/article/details/20052485?utm_source=tuicool
Recently, when using selenium to determine whether an element exists in the process of the project, I encountered a very big problem, and the following methods were used to wait for a long time each time, because there was not enough knowledge about the selenium implementation method to find the solution.
Private Boolean iselementpresent (by) {
try {
Driver.findelement (by);
return true;
} catch (Nosuchelementexception e) {
return false;
}
}
Unknown so, after tracing the source to find the start driver when the use of Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); The wait time set here is for the global setting, and the timeout for executing all commands in Webdriver is set to 30 seconds, such as the Findelement method above, the element will wait for 30 seconds by default. Sometimes it's just a matter of simply judging if an element exists and executing it immediately, and the settings here cause the script to execute slowly. The waiting time in the selenium will be put out to everyone Zhang Zhang experience.
Selenium delay wait is divided into explicit wait (Explicit wait) & implicit wait (implicit wait).
1. Wait for an explicit
Explicit wait, is clear to wait for an element to appear or is an element of the clickable conditions, etc., and so on, and so on, unless not found within the specified time, then jump out of exception.
Such as:
1 |
new WebDriverWait(driver, 15 ).until( |
2 |
ExpectedConditions.presenceOfElementLocated(By.cssSelector( "css locator" )) |
Here, 15 is the number of seconds to wait. If the condition in the until () method is not met, it will always wait here for 15 seconds, still cannot find, throws an exception.
You can also write this:
01 |
WebDriver driver = new FirefoxDriver(); |
02 |
driver.get( www.baidu.com ); |
03 |
WebElement e = ( new WebDriverWait( driver, 10 )) .until( |
04 |
new ExpectedCondition< WebElement>(){ |
06 |
public WebElement apply( WebDriver d) { |
07 |
return d.findElement( By.id( "id locator" )); |
This webelement is directly obtained through the callback function. That is, the page element.
If just want to judge whether the page is loaded into a place, you can use the first method; But if you need to get a webelement, either way, just the first way you need to get the action one more step.
4 |
return webElement.isDisplayed(); |
In addition, this way of waiting, in the loading JS code when the judgment will be more convenient, anyway I only in the code so seen, but no use, interested friends can study.
2. Implicit wait
1 |
driver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS); |
Implicit wait, the implicit wait here is for driver each execution of the command of the longest execution time can also be understood as time-out, some people misunderstand here, that is let driver wait a while, indeed some time can let driver wait a while, but the impact is global, each driver execution Cannot find the element will wait for the time set here, it is assumed that somewhere this value is set too long, must be restored after the completion of execution, or if an element exists, you will encounter a very big problem. The improved method is as follows:
Webdriver will have an implicit wait, but the parameters are only time, which leads me to what elements I need to appear, and I don't necessarily have to wait for it,
Private Boolean iselementpresent (by) {
try {
Driver.manage (). Timeouts (). implicitlywait (1, timeunit.seconds);
Driver.findelement (by);
return true;
} catch (Nosuchelementexception e) {
return false;
}
} cannot get an element, we delay ...
3. Thread hibernation
Several wait ways in selenium, pay special attention to the usage of implicitlywait