While working with web applications we would encounter scenarios where it takes some time for the page to load to perform an operation. In order to make the Selenium webdriver wait for some time before performing a new task, we take the help of synchronization.

The types of waits available in Selenium are listed below:

  • Implicit wait.
  • Explicit wait.
  • Pause execution for some time with Time class.

For demonstration of synchronization examples we shall be using the GreenKart application with the link: https://rahulshettyacademy.com/seleniumPractise/#/

Implicit Wait and its advantages

Let us input a text ber in the search edit box and the vegetable names get populated based on the text entered. Those will be added to cart with ADD TO CART button. Then PROCEED TO CHECKOUT is clicked.

The next page gets navigated where we have our order summary along with the cost details. We shall then click on the Apply button after entering promo code and obtain the Code applied..! message.

Code Implementation of the above scenario:

from selenium import webdriver

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

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

# identify element with css selector and input search criteria

driver.find_element_by_css_selector("input.search-keyword").send_keys("ber")

# wait time of 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()

# 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 Process finished with exit code 1 statement meaning test execution completed with errors. This is because after applying the promo code, there is a delay after which the promo gets successfully applied. However, there is no wait time before we try to obtain a successful application of promo code.

To overcome such a situation where we find that the Selenium code is very fast compared to application load time, we shall introduce the implicit wait for some time in our code.

In this wait, we shall set wait time for the driver object and it is applied globally till our test execution is completed. Thus the implicit wait is applicable to each step of the test execution. The method implicitly_wait is used to introduce implicit wait and the wait time in seconds is passed as an argument to the method.

Let us apply the implicit wait for five seconds. After this time is elapsed and the next 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 five seconds. This is another advantage of implicit wait.

Code Implementation with implicit wait:

from selenium import webdriver

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

# implicit wait time applied

driver.implicitly_wait(5)

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

# identify element with css selector and input search criteria

driver.find_element_by_css_selector("input.search-keyword").send_keys("ber")

# wait time of 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()

# 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 implicit 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/13248313

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

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

In the next blog post, we shall discuss how to use explicit wait along with example.


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
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"}