How to Wait for Implicit Time#
Most of Browserist's methods already implicitly wait for certain elements on the page to be ready – so you don't have to worry about it – but sometimes you may want to wait for certain elements to be ready. This is especially useful for single page application elements handled by JavaScript, but also for standard HTML that doesn't load immediately.
Wait for Item to Be Ready#
Element#
Appear#
This helper function ensures that DOM elements are ready before processing. The example waits for any H1 heading to be ready:
Python | |
---|---|
Tip
You often don't need to use this method, as most other methods already implicitly wait for elements to be ready under the hood. For example, if you want to get the text of a heading, you can simply use:
Python | |
---|---|
You don't need to specify the wait browser.wait.for_element("//xpath/to/element")
like this:
Disappear#
Conversely, you can also wait for an element to disappear from the DOM. This is useful for single page applications where elements are removed and added dynamically. An example:
Python | |
---|---|
Image#
Sometimes img
image elements are present in the DOM, but the actual images are not yet loaded after first page paint, and so they will be loaded lazily in the background. This example waits until the image(s) on the page have loaded:
Python | |
---|---|
Text#
Sometimes an element may be ready in the DOM, but the text hasn't been injectd. This helper function checks and waits for the element to contain any text:
Python | |
---|---|
Clickable#
This example is useful for elements that are present in the DOM, yet are not ready to be clicked:
Python | |
---|---|
Adjust the Timeout
Most methods have a default timeout of 5 seconds. If the element hasn't been found by then, an error is thrown. You can shorten or lengthen this by passing a timeout value in seconds. A few examples: