How do you make Selenium 2.0 wait for the page to load?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
All of these solutions are OK for specific cases, but they suffer from at least one of a couple of possible problems:
They are not generic enough -- they want you to know, ahead of time, that some specific condition will be true of the page you are going to (eg some element will be displayed)
They are open to a race condition where you use an element that is actually present on the old page as well as the new page.
Here's my attempt at a generic solution that avoids this problem (in Python):
First, a generic "wait" function (use a WebDriverWait if you like, I find them ugly):
Next, the solution relies on the fact that selenium records an (internal) id-number for all elements on a page, including the top-level
<html>
element. When a page refreshes or loads, it gets a new html element with a new ID.So, assuming you want to click on a link with text "my link" for example:
For more Pythonic, reusable, generic helper, you can make a context manager:
And then you can use it on pretty much any selenium interaction:
I reckon that's bulletproof! What do you think?
More info in a blog post about it here
If you set the implicit wait of the driver, then call the
findElement
method on an element you expect to be on the loaded page, the WebDriver will poll for that element until it finds the element or reaches the time out value.source: implicit-waits
You can try this code to let the page load completely until element is found.
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
You can use wait. there are basically 2 types of wait in selenium
- Implicit wait
This is very simple please see syntax below:
- Explicit wait
Explicitly wait or conditional wait in this wait until given condition is occurred.
You can use other properties like
visblityOf()
,visblityOfElement()
How to get Selenium to wait for page load after a click provides the following interesting approach:
WebElement
from the old page.WebElement
untilStaleElementReferenceException
is thrown.Sample code: