Using Selenium Wait Commands to Improve Page Load Tutorial

Sauce AI for Test Authoring: Move from intent to execution in minutes.|xBack to ResourcesBlogPosted

April 19, 2026 · 17 min read · Tool Comparison

Sauce AI for Test Authoring: Move from intent to execution in minutes.

|

x

Back to Resources

Blog

Posted March 8, 2023

Using Selenium Wait Commands to Improve Page Load Tutorial

Learn how to use Selenium & # x27; s implicit, explicit, and smooth wait commands to ameliorate automated examination playscript.

quote

Have you ever run an machine-driven test only to have it crash on you because of a timeout or element elision such asNoSuchElementException or ElementNotVisibleException? Don & # x27; t worry: it & # x27; s not uncommon for tests to be flaky because of extraneous constituent, with the major ones being page load multiplication.

The good news is that you can keep slow-loading Page or constituent from ram your trial by habituate one ofSelenium& # x27; s three wait commands. Using these wait commands gives the DOM time to lade HTML ingredient or pages, preventing your tests from ram as easily.

In this guide you will memorize how to use Selenium & # x27; s implicit, explicit, and fluent wait bidding to improve automated test scripts.

How to Implement Wait for Page to Load with Selenium

This tutorial starts by study a few mere Selenium tests written in Python that don & # x27; t use wait commands to determine why they & # x27; re failing. Then, it considers each of the three wait commands—implicit wait, explicit postponement, and smooth-spoken wait—and how they can improve these test.

Selenium Tests without Wait Commands

Let & # x27; s look at three Selenium exam that don & # x27; t use wait commands and regard why they & # x27; re crashing. These tests are compose forLuma, a dummy e-commerce program.

Test 1: Add Item to Cart and Change Quantity

The overarch objective of the initiatory test is to add an detail (in this case, a backpack) to the shopping cart, go to the check page, and eventually conclude the test by changing the quantity of the item.

The entire script looks like this:

1
from selenium importwebdriverfrom selenium.webdriver importKeysfrom selenium.webdriver.common.by import By
2
3
print(& quot; test example commence & quot;)
4
driver = webdriver.Chrome()
5
6
# Maximize the window sizing
7
driver.maximize_window()
8
9
# Navigate to the URL
10
driver.get(& quot; https: //magento.softwaretestingboard.com/ & quot;)
11
print(driver.title)
12
13
searchBtn = driver.find_element(By.ID,& # x27; search & # x27;)searchBtn.send_keys(& quot; backpack & quot;)
14
searchBtn.send_keys(Keys.ENTER)
15
16
backpack = driver.find_element(By.XPATH,& quot; // * [contains (text (), & # x27; Driven Backpack & # x27;)] & quot;)backpack.click()
17
18
addToCart = driver.find_element(By.ID,& # x27; product-addtocart-button & # x27;)
19
addToCart.click()
20
21
cart = driver.find_element(By.XPATH,& quot; //a [normalize-space () = & # x27; shop cart & # x27;] & quot;)
22
cart.click()
23
24
quantity = driver.find_element(By.XPATH,& quot; /html [1] /body [1] /div [2] /main [1] /div [3] /div [1] /div [2] /form [1] /div [1] /table [1] /tbody [1] /tr [1] /td [3] /div [1] /div [1] /label [1] /input [1] & quot;)
25
quantity.clear()
26
quantity.send_keys(& quot; 2 & quot;)
27
28
print(& quot; detail quantity changed successfully & quot;)

During the test run, your code will successfully run through the entire playscript until it & # x27; s time to navigate to the checkout page.

This is because you & # x27; ll hit a synchronised load screen between the merchandise page and append the particular to check. Since page loads aren & # x27; t immediate, you & # x27; ll inevitably run into matter if you & # x27; re not expend a wait bid.

In the future footstep, the quantity table on the check page should be cleared out. But due to synchronization issues, you & # x27; ll instead get aNoSuchElementException error:

1
NoSuchElementException: Message: no such element: Ineffectual to locate constituent: {& quot; method & quot;: & quot; xpath & quot;, & quot; chooser & quot;: & quot; //a [normalize-space () = & # x27; shopping cart & # x27;] & quot;}

Test 2: Get an Item in Size Large and Color Black

The objective of the 2d test is to navigate to Luma and choose a large, black discrepancy of the Hero Hoodie.

The full book looks like this:

1
from selenium import webdriver
2
from selenium.webdriver.common.by import By
3
4
print(& quot; trial case started & quot;)
5
driver = webdriver.Chrome()
6
7
# Maximize the window size
8
driver.maximize_window()
9
10
# Navigate to the URL
11
driver.get(& quot; https: //magento.softwaretestingboard.com/ & quot;)
12
print(driver.title)
13
14
largeSize = driver.find_element(By.XPATH,
15
& quot; //div [@ class= & # x27; swatch-opt-158 & # x27;] //div [@ id= & # x27; option-label-size-143-item-169 & # x27;] & quot;)
16
largeSize.click()
17
18
blackColor= driver.find_element(By.XPATH,& quot; //div [@ id= & # x27; option-label-color-93-item-49 & # x27;] & quot;)
19
blackColor.click()
20
21
addToCart = driver.find_element(By.XPATH,& quot; /html [1] /body [1] /div [2] /main [1] /div [3] /div [1] /div [2] /div [3] /div [1] /div [1] /ol [1] /li [4] /div [1] /div [1] /div [3] /div [1] /div [1] /form [1] /button [1] & quot;)
22
addToCart.click()
23
24
cart = driver.find_element(By.XPATH,& quot; //a [@ class= & # x27; action showcart & # x27;] & quot;)
25
cart.click()
26
27
topCartButton= driver.find_element((By.XPATH,& quot; //a [@ class= & # x27; activity showcart & # x27;] & quot;))
28
topCartButton.click()
29
30
product = driver.find_element(By.XPATH,& quot; //a [@ data-bind= & # x27; attr: {href: product_url}, html: product_name & # x27;] & quot;)
31
32
print(product.text)

After navigating to the dummy store, the Python script will scan the DOM to find and click the & quot; L & quot; size option for the Hero Hoodie. When it reach the size options, you & # x27; ll get aNoSuchElementException:

1
NoSuchElementException: Message: no such factor: Unable to situate element: {& quot; method & quot;: & quot; xpath & quot;, & quot; selector & quot;: & quot; //div [@ class= & # x27; swatch-opt-158 & # x27;] //div [@ id= & # x27; option-label-size-143-item-169 & # x27;] & quot;}

The reason the test will fail is because the DOM won & # x27; t be able to detect the & quot; L & quot; sizing. When corroborate the item name from the cart, it will also fail due to synchronization issue.

Test 3: Use Navigation Bar to Search Men & # x27; s Pants

The tertiary test uses the navigation bar to hover over the & quot; Men & quot; menu item, hover over the & quot; Bottoms & quot; choice, and click the & quot; Pants & quot; button.

The entire hand looks like this:

1
from selenium import webdriver
2
from selenium.webdriver importActionChains
3
from selenium.webdriver.common.by import By
4
5
print(& quot; test case started & quot;)
6
driver = webdriver.Chrome()
7
8
# Maximize the window size
9
driver.maximize_window()
10
11
# Navigate to the URL
12
driver.get(& quot; https: //magento.softwaretestingboard.com/ & quot;)
13
print(driver.title)
14
15
# The Action class, a built-in feature in Selenium that automates actions accomplish by your keyboard and shiner, hovers over the & quot; Bottoms & quot; option to reveal both the & quot; Pants & quot; and & quot; Shorts & quot; buttons
16
menTab = driver.find_element(By.XPATH,& quot; /html [1] /body [1] /div [2] /div [1] /div [1] /div [2] /nav [1] /ul [1] /li [3] /a [1] /span [2] & quot;)
17
action =ActionChains(driver)
18
action.move_to_element(menTab).perform()
19
20
bottomsButton= driver.find_element(By.ID,& quot; ui-id-18 & quot;)
21
action =ActionChains(driver)
22
action.move_to_element(bottomsButton).perform()
23
24
# Conclude the examination run by implementing a click command on the & quot; Pants & quot; button that leads you to the Men & # x27; s Pants page
25
pantsButton= driver.find_element(By.ID,& quot; ui-id-23 & quot;)
26
pantsButton.click()

When you run the test in an incorporate development environment (IDE) such asPyCharm, you & # x27; ll most immediately get a ` NoSuchElementException ` since Selenium won & # x27; t be capable to blame up the HTML element from the DOM:

1
NoSuchElementException: Message: no such element: Unable to locate element: {& quot; method & quot;: & quot; xpath & quot;, & quot; selector & quot;: & quot; /html [1] /body [1] /div [2] /div [1] /div [1] /div [2] /nav [1] /ul [1] /li [3] /a [1] /span [2] & quot;}

Because elements in seafaring bar are cover in nested HTML elements, they are prone to crash without a wait command.

SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.

Adding Selenium Wait Commands

Now, let & # x27; s see how these three tests can be improved by adding some of Selenium & # x27; s expect dictation.

Improving Test 1 with the Implicit Wait Command

The implicit wait bidding recite the driver to pause for an allotted amount of clip to allow the HTML factor to load into the DOM. You should therefore use an implicit wait if you & # x27; re conversant with the UI page and element load times.

Using an implicit wait is fabulously useful for the inaugural trial scenario above. During the test run, a synchronized loading screen between the product page and checkout page causes the trial to crash.

An implicit wait aid to handle the interruption between the two screens. By simply bestowdriver.implicitly_wait (5), the IDE waits five seconds for the synchronising to complete before moving to the future step.

The full script with the implicit wait command looks like this:

1
from selenium import webdriver
2
from selenium.webdriver import Keys
3
from selenium.webdriver.common.by import By
4
5
print(& quot; examination event get & quot;)
6
driver = webdriver.Chrome()
7
8
# Maximize the window sizing
9
driver.maximize_window()
10
11
# Navigate to the URL
12
driver.get(& quot; https: //magento.softwaretestingboard.com/ & quot;)
13
print(driver.title)
14
15
searchBtn = driver.find_element(By.ID,& # x27; lookup & # x27;)
16
searchBtn.send_keys(& quot; backpack & quot;)
17
searchBtn.send_keys(Keys.ENTER)
18
19
backpack = driver.find_element(By.XPATH,& quot; // * [contains (schoolbook (), & # x27; Driven Backpack & # x27;)] & quot;)
20
backpack.click()
21
22
addToCart = driver.find_element(By.ID,& # x27; product-addtocart-button & # x27;)
23
addToCart.click()
24
25
# Implicit wait
26
driver.implicitly_wait(5)
27
28
cart = driver.find_element(By.XPATH,& quot; //a [normalize-space () = & # x27; shopping cart & # x27;] & quot;)
29
cart.click()
30
31
quantity = driver.find_element(By.XPATH,& quot; /html [1] /body [1] /div [2] /main [1] /div [3] /div [1] /div [2] /form [1] /div [1] /table [1] /tbody [1] /tr [1] /td [3] /div [1] /div [1] /label [1] /input [1] & quot;)
32
quantity.clear()
33
quantity.send_keys(& quot; 2 & quot;)
34
print(& quot; point measure changed successfully & quot;)

Improving Test 2 with the Explicit Wait Command

The explicit wait dictation tells the driver to wait until certain conditions are present in the DOM—for instance, until an HTML factor is clickable or visible—before throwing an exception. It & # x27; s considered a more & quot; intelligent & quot; version of an implicit wait, since it allows the code to pause until certain weather are met.

However, the syntax of an explicit waiting is more complex than an inexplicit wait. You take to call the explicit wait with the undermentioned code:

1
wait =WebDriverWait(driver,<number of seconds>)

You then parameterize your explicit wait based on anticipate conditions (EC), which can involve waiting until the element is seeable or clickable, among many other possibility. Some of themost commonly used ECsduring testing are element_to_be_clickable, visibility_of_element_located, element_to_be_selected, and alert_is_present.

When writing your explicit wait, the syntax is:

1
wait.until(EC.<expected conditiontype>((By.XPATH,& quot; & lt; element xpath & gt; & quot;)))

Using an denotative waiting to improve the second examination scenario is useful in two shipway. Since the IDE has hassle detect the size option in the DOM, you can implement an expressed waiting to wait until the factor is clickable.

Another failure occurs due to synchronization issues after adding your particular to the cart itself. You can add an explicit wait until the cart element is seeable.

The entire script with explicit waits looks like this:

1
from selenium import webdriver
2
from selenium.webdriver.common.by import By
3
from selenium.webdriver.support.wait importWebDriverWait
4
from selenium.webdriver.support importexpected_conditionsas EC
5
6
print(& quot; test case commence & quot;)
7
driver = webdriver.Chrome()
8
9
# Maximize the window size
10
driver.maximize_window()
11
12
# Navigate to the URL
13
driver.get(& quot; https: //magento.softwaretestingboard.com/ & quot;)
14
print(driver.title)
15
16
# Explicit postponement
17
wait =WebDriverWait(driver,10)
18
largeSize = wait.until(EC.element_to_be_clickable((By.XPATH,& quot; //div [@ class= & # x27; swatch-opt-158 & # x27;] //div [@ id= & # x27; option-label-size-143-item-169 & # x27;] & quot;)))
19
largeSize.click()
20
21
blackColor= driver.find_element(By.XPATH,& quot; //div [@ id= & # x27; option-label-color-93-item-49 & # x27;] & quot;)
22
blackColor.click()
23
24
addToCart = driver.find_element(By.XPATH,& quot; /html [1] /body [1] /div [2] /main [1] /div [3] /div [1] /div [2] /div [3] /div [1] /div [1] /ol [1] /li [4] /div [1] /div [1] /div [3] /div [1] /div [1] /form [1] /button [1] & quot;)
25
addToCart.click()
26
27
cart = driver.find_element(By.XPATH,& quot; //a [@ class= & # x27; action showcart & # x27;] & quot;)
28
cart.click()
29
30
# Explicit wait
31
topCartButton= wait.until(EC.visibility_of_element_located((By.XPATH,& quot; //a [@ class= & # x27; activity showcart & # x27;] & quot;)))
32
topCartButton.click()
33
34
product = wait.until(EC.visibility_of_element_located((By.XPATH,& quot; //a [@ data-bind= & # x27; attr: {href: product_url}, html: product_name & # x27;] & quot;)))
35
36
print(product.text)

Improving Test 3 with the Fluent Wait Command

Alike to an explicit wait, a fluent wait tells the driver to wait until certain ECs are met in the DOM before throwing an exception. Fluent waiting are also called & quot; smart waits & quot; because they don & # x27; t wait the maximum time determine in your code; they break until the element is discoverable in the DOM.

So, if your component lade at different times—for illustration, if page load takes fifteen sec vs. a active push that takes three seconds—or if you & # x27; re unaware of how long elements take to load, you should opt for fluent waits alternatively of explicit waits.

The syntax of fluent waits is more customizable but likewise more complex than either implicit or explicit waits. It comes with two features: poll_frequency and ignored_exceptions. With poll_frequency, you can tell your script to keep rechecking constituent every limit number of bit. If you opt not to use poll_frequency, the default is 500 milliseconds.

You can use ignored_exceptions to tell your script to halt exception error (such as NoSuchElementException and ElementNotVisibleException). Use it in example element load times are long than usual, and you like to recheck the element according to poll intervals.

When writing your fluent hold, the syntax is:

1
WebDriverWait(driver,<number of sec>,poll_frequency=<figure of s>,ignored_exceptions=[<exception type>,<exception type>])
2
wait.until(EC.<expected conditiontype>((By.XPATH,& quot; & lt; element xpath & gt; & quot;)))

In the 3rd test scenario, fluent waits can be used to vacillate over the & quot; Men & quot; navigation bar option and the & quot; Bottoms & quot; option. You can use a fluent wait to make the IDE wait ten seconds before using poll_frequency to recheck the DOM every one s for the specified HTML element.

The entire codification with fluent waits looks like this:

1
from selenium import webdriver
2
from selenium.webdriver importActionChains
3
from selenium.webdriver.common.by import By
4
from selenium.webdriver.support.wait importWebDriverWait
5
from selenium.webdriver.support importexpected_conditionsas EC
6
from selenium.common importElementNotVisibleException,ElementNotSelectableException
7
8
print(& quot; trial case started & quot;)
9
driver = webdriver.Chrome()
10
11
# Maximize the window sizing
12
driver.maximize_window()
13
14
# Navigate to the URL
15
driver.get(& quot; https: //magento.softwaretestingboard.com/ & quot;)
16
print(driver.title)
17
18
# Fluent postponement
19
wait =WebDriverWait(driver,10,poll_frequency=1,ignored_exceptions=[ElementNotVisibleException,ElementNotSelectableException])
20
21
menTab = wait.until(EC.visibility_of_element_located((By.XPATH,& quot; /html [1] /body [1] /div [2] /div [1] /div [1] /div [2] /nav [1] /ul [1] /li [3] /a [1] /span [2] & quot;)))
22
action =ActionChains(driver)
23
action.move_to_element(menTab).perform()
24
25
bottomsButton= wait.until(EC.visibility_of_element_located((By.ID,& quot; ui-id-18 & quot;)))
26
action =ActionChains(driver)
27
action.move_to_element(bottomsButton).perform()
28
29
pantsButton= driver.find_element(By.ID,& quot; ui-id-23 & quot;)
30
pantsButton.click()
31
32
print(driver.title)

Are You Using Selenium Wait Commands?

Whether you & # x27; re loading a new page or research for nested HTML elements, Selenium & # x27; s look bidding can help keep your test pass from continuously crashing.

When comparing the three commands, proceed in judgement that even though using the unquestioning wait command is the simplest, it retard down test execution. Use it if you & # x27; re just getting started, but then challenge yourself to progress to explicit waits and fluent waits.

If you want the most comprehensive mechanization tool to accelerate your releases, see out. Sauce Labs lets you and rush up development with parallel. Developers can leave the hassle of lay up and maintaining infrastructure to Sauce Labs, so they can do things that matter.

Published:
Mar 8, 2023
Share this position
Copy Share Link

Need to test right now? Get started free.

Ship codification that comport exactly as it should, faster.

LinkedIn
© 2026 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks own by Sauce Labs Inc. in the United States, EU, and may be registered in former jurisdictions.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free