Selenium Wait - Explicit Wait For Presence of An Element
Waiting in selenium can be done in different ways. In this tutorial, we will use the explicit wait functionality for presence of an element given its locator. This is a way to check if the element already loaded.
But before that, please make sure you have read the
first blog on this
series to do the prerequisites.
Selenium Explicit Wait For Presence of An Elements Given Its Locator
- Create a file seleniumwaitpresenceone.py and paste the following codes
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 TimeoutException from datetime import datetime
The codes above imports the required library that we will use. - Add this line
driver = webdriver.Firefox(executable_path="geckodriver.exe")
The code above will create a webdriver instance for Firefox. - Add this line
driver.get("https://slackingslacker.github.io/seleniumindex")
The line will got to the website (https://slackingslacker.github.io/seleniumindex). - Add this function as is
def wait_for_the_element(wait_time: int, el_id: str): try: print("[{}] Finding element {}".format(str(datetime.now()), el_id)) WebDriverWait(driver, wait_time).until( EC.presence_of_all_elements_located((By.ID, el_id)) ) print("[{}] Element found".format(str(datetime.now()))) except TimeoutException as e: print("[{}] Element did not load".format(str(datetime.now())))
This method will wait for an element using locator to load at a given waiting time. It will print a message if the element loaded or not. - Add this line
wait_for_the_element(3, "navMenuId")
This line will call the method we created and will display Element found. - Add this line
wait_for_the_element(6, "noneExistentId")
This line will call the method we created and wait for 6 seconds until it gives an error. - Add this line
wait_for_the_element(9, "anotherNoneExistentId")
Again will call the method with a non existing element this time it is 9 seconds. - Add this line
driver.close()
The line will close the webdriver as well as the browser. - Run the seleniumwaitpresenceone.py. It should do the following:
- Open the firefox browser
- Browser goes to https://slackingslacker.github.io/seleniumindex
- Call the Method 3 times which prints messages in the console
- Closes the browser
Program Sample Output
[2020-06-01 13:52:53.817328] Finding element navMenuId [2020-06-01 13:52:53.886479] Element found [2020-06-01 13:52:53.886479] Finding element noneExistentId [2020-06-01 13:53:00.019786] Element did not load [2020-06-01 13:53:00.019786] Finding element anotherNoneExistentId [2020-06-01 13:53:09.055716] Element did not loadOutput explanations
- The code looks for an element given the id navMenuId
- The code found the element
- The code looks for an element given the id noneExistentId
- The code does not find the element within 6 seconds
- The code looks for an element given the id anotherNoneExistentId
- The code does not find the element within 9 seconds
Final Selenium 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 TimeoutException from datetime import datetime driver = webdriver.Firefox(executable_path="geckodriver.exe") driver.get("https://slackingslacker.github.io/seleniumindex") def wait_for_the_element(wait_time: int, el_id: str): try: print("[{}] Finding element {}".format(str(datetime.now()), el_id)) WebDriverWait(driver, wait_time).until( EC.presence_of_all_elements_located((By.ID, el_id)) ) print("[{}] Element found".format(str(datetime.now()))) except TimeoutException as e: print("[{}] Element did not load".format(str(datetime.now()))) wait_for_the_element(3, "navMenuId") wait_for_the_element(6, "noneExistentId") wait_for_the_element(9, "anotherNoneExistentId") driver.close()