Selenium has proven to be a great framework for automated testing. Automated testing speeds up the entire testing process. Although speed is a good thing when testing an app, sometimes you have to slow down and wait. Speed and deceleration are contradictory, but if you want quality and accuracy, it’s okay to wait when you need to.
Are you confused? I’m talking about the concept of “waiting” in Selenium. Since most web applications are dynamic these days, you must use wait before running tests to ensure accurate results. How do I wait for a page to load in Selenium? In this post, I will discuss what Selenium waiting is. Then I will explain the different types of waits and how to use them. Finally, I’ll show you how to use Testim to make this process easy.
What is Waiting in Selenium?
Selenium offers a number of features and methods to ensure you get everything you need to test your application. Every developer today wants their applications to be of high quality. In order to improve the quality and make the application attractive, a number of complex elements are used. Additionally, many websites display user-specific data. This means that the information on the website is dynamic and not static.
All in all, web applications are becoming more complex and user-specific. To test such applications and get accurate results on the behavior of the application, be sure to wait for the elements to load. That’s why you’re using waiting.
Selenium waits for a period of time to pause the execution of the code. This is done to wait for the elements of the application to load so that actions can be performed on them.
Next, I’ll tell you about the different types of waits and how to implement them in Python.
The type of wait in Selenium
Selenium has three main types of waits:
- Implicitly waiting
- Wait explicitly
- Smooth waiting
Implicitly waiting
Selenium Waiting for Page Load Example: Implicit waiting is a straightforward way to tell Selenium to wait. If you’re testing an application that takes time to load certain elements, you can use implicit waiting. Implicit waiting is useful when you know the approximate time it will take for an element to load.
How does Selenium wait for pages to load? Consider an example where you’re testing an application that takes 10 seconds to load elements. In this case, you know you have to make the code wait for at least ten seconds. You can use implicit wait in cases where you know a specific amount of time you have to wait for an element.
Python code
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.example.com") #This is a dummy website URL
driver.implicitly_wait(10)
elem = driver.find_element_by_name("Element_to_be_found") #This is a dummy element
I’ve used a dummy website URL and a dummy element name in the code above. You can obviously replace it with the actual website URL and element name. The above code will load a website and then wait for ten seconds. After that, it will search for a specific element by ID.
Wait explicitly
Selenium Waiting for Page Load Example: Explicit waiting is an advanced form of implicit waiting. However, when you use explicit waiting, you specify the conditions under which your code should wait.
How do I wait for a page to load in Selenium? Consider an example where you’re testing an application that displays a warning box. The app is designed in such a way that when you enter some text in the text box and click the submit button, a prompt or alert box appears. The time it takes to display an alert depends on different factors such as user input, server response time, network, etc.
If you know you have to wait for the warning box to appear, but you don’t know how long it will take for the warning box to appear, you can use explicit waiting.
Python code
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://www.example.com") #This is a dummy website URL
try:
elem = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "Element_to_be_found")) #This is a dummy element
)
finally:
driver.quit()
How does Selenium wait for pages to load? A website will open on it, and then wait ten seconds. If the code finds the element within ten seconds, an object is created. If the element is not found, an exception is thrown.
Explicit waiting can’t be used for all purposes. Some of the scenarios where you can use explicit await are:
- visibility_of
- frame_to_be_available_and_switch_to_it
- element_to_be_clickable
- staleness_of
- element_to_be_selected
- element_selection_state_to_be
- alert_is_present
Smooth waiting
You can think of Fluent Wait as an advanced version of Explicit Wait. Using Smooth Wait, you can define conditions. You can also define how often to check for a specified condition.
Consider an example where you’re testing an application that you know the elements are loading dynamically. You know that an element can take up to 50 seconds to load. But elements can be loaded at any time between zero and fifty seconds.
In this case, it is more effective to check the elements regularly than to wait for 50 seconds. In this case, you can use Smooth Waiting.
Python doesn’t have a built-in method for waiting fluently. But you can pass various parameters to the WebDriverWait method to achieve smooth waiting.
Selenium Waiting for Page to Load Example: Python Code
from selenium import webdriver
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 *
driver = webdriver.Firefox()
driver.get("http://www.example.com") #This is a dummy website URL
wait = WebDriverWait(driver, 10, poll_frequency=1, ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException])
elem = wait.until(EC.element_to_be_clickable((By.XPATH, "//div")))
The main part of this code is similar to the example used for explicit waiting. The part we need to focus on is this line
wait = WebDriverWait(driver, 10, poll_frequency=1, ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException])
Here we set the waiting time as usual. But we have to go into new territory. The first is poll_frequency, which is used to specify how often to check for a particular condition (mentioned in the next line). The second field ignore_exceptions used to mention exceptions to ignore. Because the element can be found at any time between 0 and 50 seconds, some exceptions will occur before the element is found. Here we can add those exceptions that the code will encounter if the element is not found.
The above code will open a website and wait ten seconds before throwing an exception. The code will check the element at a frequency of one second.
The challenge with Selenium Wait is choosing the type of wait that best suits your use case. You can avoid this challenge by using an automated testing tool that can have built-in waits to match elements. Let’s take a look at how to do it.
Use Testim to make it easy to wait
How do I wait for a page to load in Selenium? Testim is an automated testing tool in the Record and Playback Tests category. To use Testim for app testing, you need to have an account. You can create a free account or log in here.
Selenium Waiting for Page to Load Example: Once logged in, go to the dashboard and click on the “Create Test” button, then click on the record icon.
Now, you have to enter the URL of the website you want to test. You’ll then see another browser window open where you can perform the test. I’m going to perform a simple test on the Google search engine.
How Selenium Waits for Pages to Load: I created a test and I searched for the Testim tool on the Google search engine. Then I will click on the link to open the Testim website. After executing the test on the browser, after recording the action, the test steps recorded in the visual editor are shown in the screenshot:
To add a wait, all you need to do is hover over the action you want to add a wait for. You will see a “+” sign. Click on it.
Now you will see a menu. Scroll down to find the “Wait for Elements to Be Visible” option and click on it.
It’s as simple as that. The next time you run the test, it will wait for the element to be visible before performing the action. Additional waits can be added in a similar way to provide more flexibility to handle today’s dynamic web applications.
Selenium waits for the page to load sample summary
How do I wait for a page to load in Selenium? Now you understand that when testing dynamic applications, it’s important to use waits to get accurate results. While Selenium can help you do this, you can do it more easily using automated testing tools like Testim. The wait element is just a simple example of how easily Testim can add waits. To learn more about the waiting options offered by Testim, check out the documentation, or get a free account and explore the tool.