Selenium Wait - Explicit Wait For Visibility of An Element
Waiting in selenium can be done in different ways. In this tutorial, we will use the explicit wait functionality for visibility of elements. The element must be visible (can be seen or has at least 1px width).
But before that, please make sure you have read the
first blog on this
series to do the prerequisites.
Selenium Explicit Wait For Visibility of An Element Given Its CSS Selector
- Create a file seleniumwaitvisibilityone.py and paste the following codes
from selenium import webdriver 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#/seleniumwait")
The line will got to the website (https://slackingslacker.github.io/seleniumindex#/seleniumwait). - Add this function as is
def wait_for_the_element(wait_time: int, selector: str): try: print("[{}] Finding element {}".format(str(datetime.now()), selector)) WebDriverWait(driver, wait_time).until( EC.visibility_of_element_located((By.CSS_SELECTOR, selector)) ) 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 the element given its selector to become visible given the waiting time. It will print a message if the element is visible or not. - Add this line
wait_for_the_element(3, "div#hiddenElements > div:nth-of-type(3)")
This line will call the method we created and will display Element found. - Add this line
wait_for_the_element(6, "div#hiddenElements > div:nth-of-type(1)")
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, "div#hiddenElements > div:nth-of-type(2)")
Again will call the method with a non visible element this time it is 9 seconds. - Add this line
wait_for_the_element(12, "div#hiddenElements > div")
Again will call the method with a non visible element this time it is 12 seconds. The selector finds the first div.<div id="hiddenElements"> <div style="display: none;">This is a hidden element</div> <div style="width: 0px; height: 0px;"></div> <div>Visible div</div> </div>
The above HTML is taken from the website. - Add this line
driver.close()
The line will close the webdriver as well as the browser. - Run the seleniumwaitvisibilityone.py. It should do the following:
- Open the firefox browser
- Browser goes to https://slackingslacker.github.io/seleniumindex
- Call the Method 4 times which prints messages in the console
- Closes the browser
Program Sample Output
[2020-06-01 22:58:40.336003] Finding element div#hiddenElements > div:nth-of-type(3) [2020-06-01 22:58:40.414464] Element found [2020-06-01 22:58:40.414464] Finding element div#hiddenElements > div:nth-of-type(1) [2020-06-01 22:58:46.421484] Element did not load [2020-06-01 22:58:46.421484] Finding element div#hiddenElements > div:nth-of-type(2) [2020-06-01 22:58:55.767107] Element did not load [2020-06-01 22:58:55.767107] Finding element div#hiddenElements > div [2020-06-01 22:59:08.054460] Element did not loadOutput explanations
- The code looks for an element given the CSS selector div#hiddenElements > div:nth-of-type(3) and checks visibility
- The code found the element
- The code looks for an element given the CSS selector div#hiddenElements > div:nth-of-type(1) and checks visibility
- The code does not find the element within 6 seconds
- The code looks for an element given the CSS selector div#hiddenElements > div:nth-of-type(2) and checks visibility
- The code does not find the element within 9 seconds
- The code looks for an element given the CSS selector div#hiddenElements > div and checks visibility
- The code does not find the element within 12 seconds because the first div found is not visible
<div style="display: none;">This is a hidden element</div>
This is not the same is wait by visibility_of because the element does not need ti be present.
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#/seleniumwait") def wait_for_the_element(wait_time: int, selector: str): try: print("[{}] Finding element {}".format(str(datetime.now()), selector)) WebDriverWait(driver, wait_time).until( EC.visibility_of_element_located((By.CSS_SELECTOR, selector)) ) 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, "div#hiddenElements > div:nth-of-type(3)") wait_for_the_element(6, "div#hiddenElements > div:nth-of-type(1)") wait_for_the_element(9, "div#hiddenElements > div:nth-of-type(2)") wait_for_the_element(12, "div#hiddenElements > div") driver.close()
No comments:
Post a Comment