Explicit Wait and its advantages

Let us now try to apply explicit wait to the same scenario discussed above. The explicit wait is applicable to a specific element on the page based on certain conditions.

The explicit wait is applied with the help of the WebDriverWait class. The driver object and the wait time are passed as parameters to that class. Then we have to create an object of that class. We have to import from selenium.webdriver.support.wait import WebDriverWait package to access the methods of the WebDriverWait class in our code.

Next we shall apply until method on the wait object and pass expected_conditions along with the behavior of the element as arguments. We also have to import from selenium.webdriver.support import expected_conditions package in our code. After importing this package we will be exposed to all the expected conditions available within that package. The below image lists down some of the expected conditions available in Selenium.

Now for our scenario, where it is taking more time to get the promo code applied and to get the success message, we shall take the help of the expected condition present_of_element_located and pass the locator type and element locator value as parameters.

Syntax:

w = WebDriverWait(driver, 8)

w.until(expected_conditions.presence_of_element_located(
(By.CSS_SELECTOR,"span.promoInfo")))

It means that Selenium shall wait until the element present is located and the element is rendered in DOM. We are specifically targeting one element and waiting for that element having the css value span.promoInfo to be available in the page.

After the explicit wait time (eight seconds) has passed and the element is still not available, it shall give a timeout exception. However, if the element is available at the third second, it shall resume the execution immediately without waiting for the complete eight seconds. This is another advantage of explicit wait.

Code Implementation with Explicit Wait:

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions

from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")

# implicit wait time applied

driver.implicitly_wait(2)

driver.get("https://rahulshettyacademy.com/seleniumPractise/")

# identify element with css selector and input search criteria

driver.find_element_by_css_selector("input.search-eyword")

.send_keys("ber")

# pause execution for four seconds

time.sleep(4)

# to identify list of ADD TO CART buttons

b=driver.find_elements_by_xpath("//div[@class='product-action']/button")

# to iterate over list of buttons

for button in b:

# to click on each button

    button.click()

# identify element with css selector and click

driver.find_element_by_css_selector("img[alt='Cart']").click()

# identify element with xpath and click

driver.find_element_by_xpath("//*[text()='PROCEED TO CHECKOUT']")

.click()

# identify element with class name and input promo code

driver.find_element_by_class_name("promoCode")

.send_keys("rahulshettyacademy")

# identify element with css selector and click

driver.find_element_by_css_selector(".promoBtn").click()

# explicit wait time applied with expected condition

w = WebDriverWait(driver, 8)

w.until(expected_conditions.presence_of_element_located(

(By.CSS_SELECTOR,"span.promoInfo")))

# identify element for successful apply of promo code and print in console

print(driver.find_element_by_css_selector("span.promoInfo").text)

Output:

The output shows the Code Applied..! message printed in the console log.

Conclusion: 

Thus we have seen how to use explicit wait along with example. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248314

In the next blog post, we shall deep dive into functional automation with Python.


Tags


You may also like

API Basics and Architecture

API Basics and Architecture
Leave a Reply

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}