Latency waits of selenium are divided into explicit waits (explicit waits) and implicit waits (implicit wait ).
1. Explicit waiting
Explicit waiting means explicitly waiting for an element to appear or the clickable condition of an element to wait until it is reached, unless it is not found within the specified time, the exception is thrown out.
For example:
123 |
new WebDriverWait(driver, 15 ).until( 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 be here for 15 seconds. If it still cannot be found, an exception will be thrown.
You can also write as follows:
12345678910 |
WebDriver driver = new FirefoxDriver(); driver.get( http: //somedomain/url_that_delays_loading); WebElement e = ( new WebDriverWait( driver, 10 )) .until( new ExpectedCondition< WebElement>(){ @Override public WebElement apply( WebDriver d) { return d.findElement( By.id( "id locator" )); } } ); |
In this way, the webelement is directly obtained through the callback function, that is, the page element.
If you only want to determine whether the page is loaded to a certain place, you can use the first method. However, if you need to obtain a webelement, either method is acceptable, only the first method requires more operations.
123456 |
Wait w = new Wait(){ @Override public boolean until() { return webElement.isDisplayed(); } }; |
In addition, this waiting method makes it easier to make judgments when loading js code. I have only seen this in the code, but it has never been used. If you are interested, you can study it.
2. Implicit waiting
Implicit waiting, I think it is unreliable to wait, and I will understand it after reading a piece of code.
1 |
driver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS); |
WebDriver will perform an implicit wait, but the parameter only has time, which leads to the emergence of what elements I need. I may not be able to wait for it, but it will only delay the driver for a while. but we can also encapsulate it in a small way to make it useless:
123456789 |
public boolean isByElementDisplayed(By by, int time) { boolean status = false ; if (driver.findElement(by).isDisplayed() == false ) { driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS); } else { status = true ; } return status; } |
If we don't get a certain element, we will delay it...
Finally, there is one of the most inefficient practices:
Not recommended.
For the first article, I 'd like to give you more comments.
Selenium delay Wait [Switch]