Selenium Wait - Explicit Wait For Element Selection Using Locator
Waiting in selenium can be done in different ways. In this tutorial, we will use the explicit wait functionality for waiting if the element is to be selected using locator.
But before that, please make sure you have read the
first blog on this
series to do the prerequisites.
Selenium Explicit Wait For Selection Using Locator
- Create a file seleniumwaitselection.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#/seleniumwait")
The line will got to the website (https://slackingslacker.github.io/seleniumindex#/seleniumwait). - Add this function as is
def wait_for_element_selection(wait_time: int, selector: str, is_selected: bool): try: print("[{}] Waiting element {}".format(str(datetime.now()), selector)) WebDriverWait(driver, wait_time).until( EC.element_located_selection_state_to_be((By.CSS_SELECTOR, selector), is_selected) ) print("[{}] Element found".format(str(datetime.now()))) except TimeoutException as e: print("[{}] Element not found".format(str(datetime.now())))
This method will wait for the element to be checked. It will print a message if the element did loaded or not. - Add this line
wait_for_element_selection(3, "div#elementSelection input[name='forSelected']", True)
This line will call the method we created and will wait within 3 seconds until it displays Element found. True parameter means that the checkbox must be checked. - Add this line
wait_for_element_selection(6, "div#elementSelection input[name='forSelected']", False)
This line will call the method we created and will wait within 6 seconds until it displays Element not found. False parameter means that the checkbox must NOT be checked. - Add this line
wait_for_element_selection(9, "div#elementSelection input[name='notSelected']", True)
This line will call the method we created and will wait within 9 seconds until it displays Element not found. True parameter means that the checkbox must NOT be checked. - Add this line
wait_for_element_selection(12, "div#elementSelection input[name='notSelected']", False)
This line will call the method we created and will wait within 12 seconds until it displays Element found. False parameter means that the checkbox must be checked. - Add this line
driver.close()
The line will close the webdriver as well as the browser. - Run the seleniumwaitselection.py. It should do the following:
- Open the firefox browser
- Browser goes to https://slackingslacker.github.io/seleniumindex#/seleniumwait
- Call the Method 4 time which prints messages in the console
- Closes the browser
Program Sample Output
[2020-06-12 23:19:12.181093] Waiting element div#elementSelection input[name='forSelected'] [2020-06-12 23:19:12.250153] Element found [2020-06-12 23:19:12.250153] Waiting element div#elementSelection input[name='forSelected'] [2020-06-12 23:19:18.562138] Element not found [2020-06-12 23:19:18.562138] Waiting element div#elementSelection input[name='notSelected'] [2020-06-12 23:19:27.931689] Element not found [2020-06-12 23:19:27.931689] Waiting element div#elementSelection input[name='notSelected'] [2020-06-12 23:19:27.947199] Element foundOutput explanations
- The code waits for a given the CSS selector div#elementSelection input[name='forSelected'] that is checked.
- The code found the a checkbox is checked
- The code waits for a given the CSS selector div#elementSelection input[name='forSelected'] that is NOT checked.
- The code did not find the a checkbox is not checked within 6 seconds
- The code waits for a given the CSS selector div#elementSelection input[name='notSelected'] that is checked.
- The code did not find the a checkbox is checked
- The code waits for a given the CSS selector div#elementSelection input[name='notSelected'] that is NOT checked.
- The code found the checkbox is NOT checked within 12 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#/seleniumwait") def wait_for_element_selection(wait_time: int, selector: str, is_selected: bool): try: print("[{}] Waiting element {}".format(str(datetime.now()), selector)) WebDriverWait(driver, wait_time).until( EC.element_located_selection_state_to_be((By.CSS_SELECTOR, selector), is_selected) ) print("[{}] Element found".format(str(datetime.now()))) except TimeoutException as e: print("[{}] Element not found".format(str(datetime.now()))) wait_for_element_selection(3, "div#elementSelection input[name='forSelected']", True) wait_for_element_selection(6, "div#elementSelection input[name='forSelected']", False) wait_for_element_selection(9, "div#elementSelection input[name='notSelected']", True) wait_for_element_selection(12, "div#elementSelection input[name='notSelected']", False) driver.close()
No comments:
Post a Comment