Selenium wait mechanism
In Selenium, the wait mechanism is key to ensuring that page elements are fully loaded before performing operations.
Because web page loading speed is affected by factors such as network and server performance, directly operating on elements that have not finished loading will cause the script to fail.
Selenium provides multiple wait mechanisms to solve this problem.
1. Implicit wait (implicitly_wait)
1.1 What is an implicit wait?
An implicit wait is a global wait mechanism that waits a certain amount of time when locating elements. If the element is found within the specified time, Selenium continues immediately; if it times out without finding the element, it throws NoSuchElementException exception.
1.2 How to use an implicit wait?
An implicit wait is set via the implicitly_wait() method. This method only needs to be called once, and all subsequent element lookups will follow this wait time.
Example
from
selenium
import
webdriver
from
selenium.
webdriver
.
chrome
.
service
import
Service
as
ChromeService
Set the correct driver path
service
=
ChromeService
(
executable_path
=
"./chromedriver-mac-arm64/chromedriver"
)
options
=
webdriver.
ChromeOptions
(
)
driver
=
webdriver.
Chrome
(
service
=
service
,
options
=
options
)
Set the implicit wait time to 10 seconds
driver.
implicitly_wait
(
10
)
Open a web page
driver.
get
(
"https://example.com"
)
Find elements
element
=
driver.
find_element_by_id
(
"element_id"
)
Close the browser
driver.
quit
(
)
1.3 Advantages and disadvantages of implicit waits
Fixed size, stores elements of the same type.
- Simple and easy to use; set once and applied to all element lookups.
- Suitable for most simple scenarios.
High efficiency in random access of elements.
- Global wait, which may cause unnecessary waiting time.
- Cannot handle some complex wait conditions, such as waiting for an element to become clickable.
2. Explicit wait (WebDriverWait and expected_conditions)
2.1 What is an explicit wait?
An explicit wait is a more flexible wait mechanism that lets you set wait conditions for specific operations. Explicit waits are typically used together with the WebDriverWait class and expected_conditions module.
2.2 How to use an explicit wait?
The basic usage of an explicit wait is as follows:
Example
from
selenium
import
webdriver
from
selenium.
webdriver
.
chrome
.
service
import
Service
as
ChromeService
from
selenium.
webdriver
.
common
.
by
import
By
from
selenium.
webdriver
.
support
.
ui
import
WebDriverWait
from
selenium.
webdriver
.
support
import
expected_conditions
as
EC
Set the correct driver path
service
=
ChromeService
(
executable_path
=
"./chromedriver-mac-arm64/chromedriver"
)
options
=
webdriver.
ChromeOptions
(
)
driver
=
webdriver.
Chrome
(
service
=
service
,
options
=
options
)
Open a web page
driver.
get
(
"https://example.com"
)
Set an explicit wait, waiting up to 10 seconds until the element appears
element
=
WebDriverWait
(
driver
,
10
)
.
until
(
EC.
presence_of_element_located
(
(
By.
ID
,
"element_id"
)
)
)
Close the browser
driver.
quit
(
)
2.3 Commonly used expected_conditions
expected_conditions module provides various predefined wait conditions; here are some commonly used ones:
presence_of_element_located: Wait for the element to appear in the DOM.visibility_of_element_located: Wait for the element to appear in the DOM and be visible.element_to_be_clickable: Wait for the element to be clickable.text_to_be_present_in_element: Wait for the element's text to contain the specified text.
2.4 Advantages and disadvantages of explicit waits
Fixed size, stores elements of the same type.
- High flexibility; different wait conditions can be set for different operations.
- Can handle complex wait scenarios.
High efficiency in random access of elements.
- The code is relatively complex and requires more code.
- Need to set wait conditions individually for each operation.
3. Fixed wait (time.sleep)
3.1 What is a fixed wait?
A fixed wait is the simplest wait mechanism; it uses the time.sleep() method to pause script execution for a specified time. Regardless of whether the page has finished loading, the script waits the specified time before continuing.
3.2 How to use a fixed wait?
Example
import
time
from
selenium
import
webdriver
from
selenium.
webdriver
.
chrome
.
service
import
Service
as
ChromeService
Set the correct driver path
service
=
ChromeService
(
executable_path
=
"./chromedriver-mac-arm64/chromedriver"
)
options
=
webdriver.
ChromeOptions
(
)
driver
=
webdriver.
Chrome
(
service
=
service
,
options
=
options
)
Open a web page
driver.
get
(
"https://example.com"
)
Fixed wait of 5 seconds
time
.
sleep
(
5
)
Find elements
element
=
driver.
find_element_by_id
(
"element_id"
)
Close the browser
driver.
quit
(
)
3.3 Advantages and disadvantages of fixed waits
Fixed size, stores elements of the same type.
- Simple and easy to use, suitable for simple test scenarios.
High efficiency in random access of elements.
- Inefficient, may cause unnecessary waiting time.
- Cannot dynamically adjust the wait time based on page loading.
4. Fluent Wait
**Effect:** Dynamically set wait conditions, with customizable polling frequency and the ability to ignore specific exceptions.
**Method:** WebDriverWait combined with polling_every and ignoring
**Feature:** More flexible, suitable for scenarios requiring dynamic adjustment of wait time.
Example
from
selenium
import
webdriver
from
selenium.
webdriver
.
chrome
.
service
import
Service
as
ChromeService
from
selenium.
webdriver
.
common
.
by
import
By
from
selenium.
webdriver
.
support
.
ui
import
WebDriverWait
from
selenium.
webdriver
.
support
import
expected_conditions
as
EC
from
selenium.
common
.
exceptions
import
NoSuchElementException
Set the correct driver path
service
=
ChromeService
(
executable_path
=
"./chromedriver-mac-arm64/chromedriver"
)
options
=
webdriver.
ChromeOptions
(
)
driver
=
webdriver.
Chrome
(
service
=
service
,
options
=
options
)
driver.
get
(
"https://www.example.com"
)
Set up Fluent Wait
wait
=
WebDriverWait
(
driver
,
timeout
=
10
,
poll_frequency
=
1
,
ignored_exceptions
=
[
NoSuchElementException
]
)
element
=
wait.
until
(
EC.
presence_of_element_located
(
(
By.
ID
,
"username"
)
)
)
5. Summary
- Implicit wait: Suitable for simple scenarios; global wait, but may cause unnecessary waiting time.
- Explicit wait: Suitable for complex scenarios; highly flexible, but the code is relatively complex.
- Fixed wait: Simple and easy to use, but inefficient; not recommended for complex test scenarios.
- Fluent Wait:: Suitable for scenarios requiring dynamic adjustment of wait time.
In real automated testing, it is recommended to choose an appropriate wait mechanism based on specific testing needs. Typically, explicit waits are the most recommended approach because they offer better flexibility and efficiency.
6. Comprehensive Example
Below is a complete example demonstrating how to use implicit and explicit waits:
Example
from
selenium
import
webdriver
from
selenium.
webdriver
.
chrome
.
service
import
Service
as
ChromeService
from
selenium.
webdriver
.
common
.
by
import
By
from
selenium.
webdriver
.
support
.
ui
import
WebDriverWait
from
selenium.
webdriver
.
support
import
expected_conditions
as
EC
Set the correct driver path
service
=
ChromeService
(
executable_path
=
"./chromedriver-mac-arm64/chromedriver"
)
options
=
webdriver.
ChromeOptions
(
)
driver
=
webdriver.
Chrome
(
service
=
service
,
options
=
options
)
Set implicit wait
driver.
implicitly_wait
(
5
)
Open a web page
driver.
get
(
"https://www.example.com"
)
Use explicit wait to wait for the element to be visible
username
=
WebDriverWait
(
driver
,
10
)
.
until
(
EC.
visibility_of_element_located
(
(
By.
ID
,
"username"
)
)
)
username.
send_keys
(
"testuser"
)
Use explicit wait to wait for the element to be clickable
submit_button
=
WebDriverWait
(
driver
,
10
)
.
until
(
EC.
element_to_be_clickable
(
(
By.
ID
,
"submit-btn"
)
)
)
submit_button.
click
(
)
Close the browser
driver.
quit
(
)