waiting is a crucial concept when
automating web interactions. It helps ensure that your script waits for
certain conditions to be met before proceeding to the next steps. This is
especially important because web elements might take varying amounts of time
to load or change on a page.
There are two main types of waits in Selenium:
- Implicit wait
- Explicit wait
Implicit Wait:
The implicit wait is a global setting that
applies to all elements on the page. It tells the Selenium WebDriver
to wait for a certain amount of time before throwing an exception if an
element is not immediately available. You set the implicit wait time using
the implicitly_wait() method.
- Example:
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- driver = webdriver.Chrome()
- driver.implicitly_wait(10)
- driver.get("https://www.google.com/")
- driver.find_element(By.NAME, "q").send_keys("testingcolleges")
- driver.find_element(By.NAME, "btnK").click()
Explicit Wait:
An explicit wait makes Webdriver wait for specific conditions to be met for a particular element.
To use Explicit Wait we need to import
"from selenium.webdriver.support.wait import WebDriverWait".
You can use the WebDriverWait class in combination
with expected conditions from the expected_conditions module to achieve
explicit wait. For that, we need to import
"from selenium.webdriver.support import expected_conditions".
- Example:
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.wait import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- driver = webdriver.Chrome()
- wait = WebDriverWait(driver, timeout=10)
- driver.get("https://www.google.com/")
- driver.find_element(By.NAME, "q").send_keys("testingcolleges")
- wait.until(EC\
- .element_to_be_clickable(driver.find_element(By.NAME, "btnK")))\
- .click()
In the above example, we will give timeout=10 and the excepted condition as element_to_be_clickable. That means the driver
will wait up to 10 seconds until the given element to be clickable.
Depending on the scenario use different excepted condition
functions.
Excepted conditions are;
Functions | Description |
---|---|
alert_is_present() | An expectation for checking if an alert is currently present and switching to it |
all_of(*expected_conditions) | An expectation that all of multiple expected conditions is true. |
any_of(*expected_conditions) | An expectation that any of multiple expected conditions is true. |
element_attribute_to_include(locator, str], …) | An expectation for checking if the given attribute is included in the specified element. |
element_located_selection_state_to_be(…) | An expectation to locate an element and check if the selection state specified is in that state. |
element_located_to_be_selected(locator, str]) | An expectation for the element to be located is selected. |
element_selection_state_to_be(element, …) | An expectation for checking if the given element is selected. |
element_to_be_clickable(mark, Tuple[str, str]]) | An Expectation for checking an element is visible and enabled such that you can click it. |
element_to_be_selected(element) | An expectation for checking the selection is selected. |
frame_to_be_available_and_switch_to_it(…) | An expectation for checking whether the given frame is available to switch to. |
invisibility_of_element(element, Tuple[str, …) | An Expectation for checking that an element is either invisible or not present on the DOM. |
invisibility_of_element_located(locator, …) | An Expectation for checking that an element is either invisible or not present on the DOM. |
new_window_is_opened(current_handles) | An expectation that a new window will be opened and have the number of windows handles increase. |
none_of(*expected_conditions) | An expectation that none of 1 or multiple expected conditions is true. |
number_of_windows_to_be(num_windows) | An expectation for the number of windows to be a certain value. |
presence_of_all_elements_located(locator, str]) | An expectation for checking that there is at least one element present on a web page. |
presence_of_element_located(locator, str]) | An expectation for checking that an element is present on the DOM of a page. |
staleness_of(element) | Wait until an element is no longer attached to the DOM. |
text_to_be_present_in_element(locator, str], …) | An expectation for checking if the given text is present in the specified element. |
text_to_be_present_in_element_attribute(…) | An expectation for checking if the given text is present in the element’s attribute. |
text_to_be_present_in_element_value(locator, …) | An expectation for checking if the given text is present in the element’s value. |
title_contains(title) | An expectation for checking that the title contains a case-sensitive substring. |
title_is(title) | An expectation for checking the title of a page. |
url_changes(url) | An expectation for checking the current url. |
url_contains(url) | An expectation for checking that the current url contains a case- sensitive substring. |
url_matches(pattern) | An expectation for checking the current url. |
url_to_be(url) | An expectation for checking the current url. |
visibility_of(element) | An expectation for checking that an element, known to be present on the DOM of a page, is visible. |
visibility_of_all_elements_located(locator, str]) | An expectation for checking that all elements are present on the DOM of a page and visible. |
visibility_of_any_elements_located(locator, str]) | An expectation for checking that there is at least one element visible on a web page. |
visibility_of_element_located(locator, str]) | An expectation for checking that an element is present on the DOM of a page and visible. |
Can we use explicit wait without the expected_condition module?
Yes, you can use explicit wait without the 'expected_conditions' module by creating custom conditions using 'WebDriverWait' along with lambda functions. While 'expected_conditions' provides a convenient
way to express common conditions, using custom conditions can give you more
flexibility in waiting for specific elements or situations.
Here's how you can use explicit wait with a custom condition using a lambda
function:
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.wait import WebDriverWait
- driver = webdriver.Chrome()
- wait = WebDriverWait(driver, 10)
- # Define a custom condition using a lambda function
- custom_condition = lambda driver: driver.find_element(By.ID, "element_id").is_displayed()
- element = wait.until(custom_condition)
- # Now you can interact with the element once it's located and displayed
In the above example, the custom condition is a lambda function that checks if the element with the specified ID is displayed. You can modify the lambda function to suit your specific waiting criteria.
However, keep in mind that the expected_conditions module provides a range of pre-defined conditions that cover common use cases, such as element visibility, element presence, element clickability, and more. Using these conditions can often make your code more readable and maintainable. But if you have unique or complex waiting requirements, creating custom conditions using lambda functions is a viable option.
No comments:
Post a Comment