Selenium Wait - Explicit Wait For Element To Be Selected 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 Selected Using Locator
- Create a file seleniumwaitselected.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_selected(wait_time: int, selector: str): try: print("[{}] Waiting element {}".format(str(datetime.now()), selector)) WebDriverWait(driver, wait_time).until( EC.element_located_to_be_selected((By.CSS_SELECTOR, selector)) ) print("[{}] Element selected".format(str(datetime.now()))) except TimeoutException as e: print("[{}] Element not selected".format(str(datetime.now())))
This method will wait for the element to be selected. It will print a message if the element did loaded or not. - Add this line
wait_for_element_selected(3, "div#elementSelected input[name='forSelectedRadio']")
This line will call the method we created and will wait within 3 seconds until it displays Element selected. - Add this line
wait_for_element_selected(6, "div#elementSelected input[name='notSelectedRadio']")
Again will call the method we created and wait for 6 seconds until it gives a message Element not selected. - Add this line
driver.close()
The line will close the webdriver as well as the browser. - Run the seleniumwaitselected.py. It should do the following:
- Open the firefox browser
- Browser goes to https://slackingslacker.github.io/seleniumindex#/seleniumwait
- Call the Method 2 time which prints messages in the console
- Closes the browser
Program Sample Output
[2020-06-12 23:08:18.550321] Waiting element div#elementSelected input[name='forSelectedRadio'] [2020-06-12 23:08:18.612853] Element selected [2020-06-12 23:08:18.612853] Waiting element div#elementSelected input[name='notSelectedRadio'] [2020-06-12 23:08:24.855407] Element not selectedOutput explanations
- The code looks for an element given the CSS selector div#elementSelected input[name='forSelectedRadio'] that is selected
- The code found the a selected radio button
- The code looks for an element given the CSS selector div#elementSelected input[name='notSelectedRadio'] that is selected
- The code does not find the element within 6 seconds because the radio button is not selected
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_selected(wait_time: int, selector: str): try: print("[{}] Waiting element {}".format(str(datetime.now()), selector)) WebDriverWait(driver, wait_time).until( EC.element_located_to_be_selected((By.CSS_SELECTOR, selector)) ) print("[{}] Element selected".format(str(datetime.now()))) except TimeoutException as e: print("[{}] Element not selected".format(str(datetime.now()))) wait_for_element_selected(3, "div#elementSelected input[name='forSelectedRadio']") wait_for_element_selected(6, "div#elementSelected input[name='notSelectedRadio']") driver.close()
No comments:
Post a Comment