+91 9790664230
kumar.arun211@gmail.com

How to work with expected conditions explicit wait

The Blog to Learn Selenium

How to work with expected conditions explicit wait

These days most of the web applications are using AJAX/Dynamic loading/lazy loading techniques. After loading the web page in the browser, elements within that page may load at different time intervals. This makes locating elements difficult, if an element is not yet present in the DOM, a locate function will raise an ElementNotVisibleException exception. We can solve this issue using waits.

Selenium WebDriver provides two types of waits – implicit & explicit.

An explicit wait makes WebDriver wait for a certain condition to occur before proceeding further with execution. To know more about implicit wait, please visit our article here. There are some default and convenience methods available in selenium package that will help you to write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

Expected Conditions:

There are some common conditions that you face frequently when automating web browsers. Below is the list and explanation of some of Selenium Python binding provided methods in expected_condition class.

class name is ‘selenium.webdriver.support.expected_conditions
So you have to import this as ‘from selenium.webdriver.support import expected_conditions as ec’

Note that ‘as ec’. This makes the ‘expected_conditions’ term shorter and easy to use.

All the example here uses a sample page. Download and keep it in your local path to use it in scripts.

alert_is_present

Expects an alert to be present on the web page.

Parameters: No parameters required

Returns:
alert object if alert is present on the web page. Otherwise throws selenium.common.exceptions.TimeoutException.

Example:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

driver = webdriver.Chrome()
driver.get("path to test-page.html")
driver.find_element_by_id("show-alert").click()
wait = WebDriverWait(driver, 10)
alert = wait.until(ec.alert_is_present())
alert.accept()
driver.close()

element_located_selection_state_to_be(locator, is_selected)

Expects an element identified by the locator to be present in the web page and checks whether the expected element is in the selection state specified.

Parameters:
locator – It is a tuple of (by, path)
is_selected – It is a boolean value. True – Element should be in selected state. False – Element should not be in selected state.

Returns:
True – when the element is found and in expected selection state before max wait time.
Throws selenium.common.exceptions.TimeoutException when the element is not found or not in expected selection state after wait time.

Example:

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

driver = webdriver.Chrome()
driver.get("path to test-page.html")
wait = WebDriverWait(driver, 10)

result = wait.until(ec.element_located_selection_state_to_be((By.ID, 'subscribe-checkbox-selected'), True))
#change True to False to see TimeoutException
print result

result = wait.until(ec.element_located_selection_state_to_be((By.ID, 'terms-checkbox-notselected'), False))
#change False to True to see TimeoutException
print result
driver.close()

element_located_to_be_selected(locator)

Expects an element identified by the locator to be present in the web page and expects the element to be in selected state.

Parameters:
locator – It is a tuple of (by, path)

Returns:
True – when the element is found and in selected state
Throws selenium.common.exceptions.TimeoutException when the element is not found or not in selected state after wait time.

Example:

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

driver = webdriver.Chrome()
driver.get("path to test-page.html")
wait = WebDriverWait(driver, 10)
result = wait.until(ec.element_located_to_be_selected((By.ID, 'subscribe-checkbox-selected')))
print result

driver.close()

element_selection_state_to_be(element, is_selected)

Expects an element in the web page to be in a specified selection state. Same as ‘element_located_selection_state_to_be’ except here we directly pass web element instead of locator.

Parameters:
element – web element
is_selected – It is a boolean value. True – Element should be in selected state. False – Element should not be in selected state.

Returns:
True – when the element state becomes expected selection state before max wait time
Throws selenium.common.exceptions.TimeoutException when the element is not in expected selection state after wait time.

Example:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

driver = webdriver.Chrome()
driver.get("path to test-page.html")

subscribe_checkbox = driver.find_element_by_id('subscribe-checkbox-selected')
wait = WebDriverWait(driver, 10)
result = wait.until(ec.element_selection_state_to_be(subscribe_checkbox, True))
print result
driver.close()

element_to_be_clickable(locator)

Wait for an element identified by the locator is enabled and visible such that you can click on it. Note that element should be in visible state.

Parameters:
locator – It is a tuple of (by, path)

Returns:
element – Returns the element identified by the given locator when it becomes clickable.
Throws selenium.common.exceptions.TimeoutException when the element is not found or not in clickable state after wait time.

Example:

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

driver = webdriver.Chrome()
driver.get("path to test-page.html")
wait = WebDriverWait(driver, 10)
element = wait.until(ec.element_to_be_clickable((By.ID,'disabled-register-button')))
print element
element.click()
driver.close()

element_to_be_selected(element)

Expects an element in the web page to be in selected state. Same as ‘element_located_to_be_selected’ except here we directly pass web element instead of locator.

Parameters:
element – web element

Returns:
True – when the elements selection state becomes expected selection state before max wait time.
Throws selenium.common.exceptions.TimeoutException when the element is not in expected selection state after wait time.

Example:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

driver = webdriver.Chrome()
driver.get("path to test-page.html")

subscribe_checkbox = driver.find_element_by_id('subscribe-checkbox-selected')
wait = WebDriverWait(driver, 10)
result = wait.until(ec.element_to_be_selected(subscribe_checkbox))
print result
driver.close()

From these example, we could understand following

1. when the argument is ‘locator’ you need to pass tuble of (by, path)
2. when the argument is ‘element’ you have to find and pass the web element.
3. return values are different for each method

There could be variations from above observation but they are self explanatory.

frame_to_be_available_and_switch_to_it(locator)

Wait for the given frame to be available and switch to it. If the frame becomes available before wait time, switches the given driver to the specified frame and returns the driver. Otherwise throws TimeoutException.

invisibility_of_element(locator)

Wait for an element identified by the locator to go invisible or get removed on the DOM. Here you can pass element directly as well instead of locator, invisibility_of_element(element). If the element is removed or not visible in DOM, returns True. Otherwise throws TimeoutException.

There are many other default methods available in expected_conditions class for which we will come up with follow up article.  Thanks for reading and please provide your thoughts in comments section.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2019 Copyrights. All Rights Reserved. Arunkumar Velusamy