In this Python Selenium article we want to learn about Implicit and Explicit Waits in Python Selenium, when you are working with browser automation using Python Selenium, it is important to handle delays and synchronization issues that may arise during web page interactions. two common techniques for managing timing in Selenium are implicit and explicit waits. in this article we want to talk about both of them.
To work the examples of this tutorial you need some requirements, first you should have installed Python in your system, then we need to install Python Selenium and you can use pip for that like this.
1 |
pip install selenium |
Note: You can download the drivers from here.
Python Selenium Implicit Waits
Implicit waits tell the Selenium WebDriver to wait for a certain amount of time when trying to find an element or perform an action. if the element is not immediately available, Selenium will wait for the specified time before throwing an exception. This wait is applied globally to all elements.
For setting an implicit wait in Selenium, we need to set the implicitly_wait() method on the driver object like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from selenium import webdriver # Launch the browser driver = webdriver.Chrome() # Set the implicit wait to 10 seconds driver.implicitly_wait(10) # Open Google.com driver.get("https://www.google.com") # Perform actions after the implicit wait # ... |
In this example, we set the implicit wait to 10 seconds. If any element is not immediately found when performing actions on the web page, Selenium will wait for up to 10 seconds before raising a NoSuchElementException exception.
Python Selenium Explicit Waits
Explicit waits are more granular and allows us to define specific conditions to wait for before proceeding with further actions. we can specify conditions such as element visibility, element clickability or custom conditions defined by us.
For using an explicit wait in Selenium, we need to import the WebDriverWait and expected_conditions classes like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # Launch the browser driver = webdriver.Chrome() # Open Google.com driver.get("https://www.google.com") # Set the explicit wait to 10 seconds wait = WebDriverWait(driver, 10) # Wait for the search input element to be visible search_input = wait.until(EC.visibility_of_element_located((By.NAME, "q"))) # Perform actions after the element is visible search_input.send_keys("Selenium") # ... |
In this example, we have created an instance of WebDriverWait with a timeout of 10 seconds. after that we have used the until() method with the desired expected condition. In here we wait until the search input element with the name q is visible on the page. Once the element is visible, we can proceed with further actions.
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # Implicit Wait def implicit_wait_example(): # Launch the browser driver = webdriver.Chrome() # Set implicit wait to 10 seconds driver.implicitly_wait(10) # Open Googl.com driver.get("https://www.google.com") # Perform actions after implicit wait # ... # Close browser driver.close() # Explicit Wait def explicit_wait_example(): # Launch th browser driver = webdriver.Chrome() # Open Google.com driver.get("https://www.google.com") # Set the explicit wait to 10 seconds wait = WebDriverWait(driver, 10) # Wait for the search input element to be visible search_input = wait.until(EC.visibility_of_element_located((By.NAME, "q"))) # Perform actions after the element is visible search_input.send_keys("Selenium") # Close the browser driver.close() # Run the examples implicit_wait_example() explicit_wait_example() |