Norway


This tutorial will guide you through the concept of Webdriver waits (Implicit and Explicit Waits) and how to use them in Python.

If you are doing automation in Selenium Python and wish to write error-free scripts, then you must learn to use waits. They let you handle any unexpected condition which may occur.

While automating a application, you intend to write a script which can execute the following tasks.

Click here to Go Back to main Selenium Python tutorial.

  • Load up the browser,
  • Open up the website,
  • Locate the elements, and
  • Execute the needed action.

However, the test script could sometimes fail due to the following reasons.

  • The web element was not present as it could not load due to the runtime issues.
  • The usage of AJAX techniques in the web applications has introduced uncertainty in the sense that loading of the web page and the web elements present in it may happen at a different span.
  • Many times, due to peak traffic or network latency, our application may behave differently than it does at normal, optimal conditions.

In these cases, we need to wait for the time to allow the web elements to load completely, before using them in our tests. Webdriver API provides two types of wait mechanisms to handle such conditions. We will discuss these wait conditions one by one in detail.

Selenium Webdriver Waits in Python

Selenium WebDriver Waits in Python Explained with Examples  - Selenium WebDriver Waits in Python Explained with Examples - Selenium WebDriver Waits in Python Explained with Examples

The two types of Selenium Webdriver waits are :

  • Implicit Wait
  • Explicit Wait

Implicit Wait

An implicit wait directs the WebDriver to poll the DOM for a certain amount of time (as mentioned in the command) when trying to locate an element that is not visible immediately. The default value of time that can be set using Implicit wait is zero. Its unit is in seconds. Implicit wait remains associated with the web element until it gets destroyed.

Follow the below coding snippet illustrating the use of Implicit wait in Python.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.implicitly_wait(100)

driver.get("http://google.com")
driver.maximize_window()

print("Implicit Wait Example")

inputElement = driver.find_element_by_id("lst-ib")
inputElement.send_keys("Techbeamers")
inputElement.submit()

driver.close()

In the above code, the implicitly_wait( ) method tells the Webdriver to poll the DOM again and again for a certain amount of time. The timeout in this example is 100 seconds which will trigger if the target element is not available during that period.

We define Implicit wait for a Webdriver object so it will remain active until the existence of that object.

Explicit Wait

There may be scenarios when the wait time is uncertain. Explicit waits work as a savior there. Here we can define certain conditions, and Selenium WebDriver will proceed with the execution of the next step only after this condition gets fulfilled.

The explicit wait is the most preferred way of implementing Selenium webdriver waits in a test script. It provides the flexibility to wait for a custom condition to happen and then move to the next step.

Following are the two Selenium Python classes needed to implement explicit waits.

  • WebDriverWait, and
  • Expected Conditions class of the Python.

To understand its usage, let’s take an example of a Simple JavaScript alert on a webpage. A button is present on the web page to trigger that alert.

One of the use cases would be to click on the button and verify that it fires the alert on not. Using the Explicit wait with Expected conditions WebDriver will wait for the time only till the Simple JavaScript is not visible on the screen. As soon as it appears, the WebDriver will move on to execute the next step.

However, if the alert does not appear until the maximum wait time defined in the explicit wait, Webdriver will throw a “NoAlertPresentException.”

Here is the HTML code for generating a simple alert using JavaScript.

<body bgcolor="#C0C0C0">
<h1>Simple Alert Demonstration</h1>
<p>
Press button underneath to generate an alert.
</p>
<button onclick="alertFunction()" name ="alert">Generate Alert</button>
<script>
function alertFunction() {
    alert("Hello! I'm a Simple Alert.nPlease press the 'OK' button.");
}
</script>
</body>
</html>

You need to save this file on your computer and name it as the ‘Simple_Alert.HTML.’

Following is the code snippet demonstrating the Explicit wait.

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 cond
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import TimeoutException

driver = webdriver.Firefox()
driver.maximize_window()
# = "file://<Specify Path to Simple_Alert.HTML>"
 = "file://C:/Users/Automation-Dev/Desktop/Meenakshi/Selenium%20Python/Simple_Alert.HTML"
driver.get()

#Press the "Alert" button to demonstrate the Simple Alert
button = driver.find_element_by_name('alert')
button.click()

try:
    # Wait as long as required, or maximum of 10 sec for alert to appear
    WebDriverWait(driver,10).until(cond.alert_is_present())

    #Change over the control to the Alert window
    obj = driver.switch_to.alert

    #Retrieve the message on the Alert window
    msg=obj.text
    print ("Alert shows following message: "+ msg )
    
    #Use the accept() method to accept the alert
    obj.accept()

except (NoAlertPresentException, TimeoutException) as py_ex:
    print("Alert not present")
    print (py_ex)
    print (py_ex.args)
finally:
    driver.quit()

In the above script, the timeout value is 10 seconds. It means the Webdriver will wait for 10 seconds before throwing a TimeoutException.
If the element is present, then it may return within the 0 – 10 seconds. By default, the WebDriverWait API executes the ExpectedCondition every 500 milliseconds until it returns successfully.

If the ExpectedCondition matches, then the return value will be a Boolean (TRUE) whereas a non-null value for all other ExpectedCondition types.

Standard Expected Conditions

There are a no. of standard conditions which you may commonly encounter while automating the web pages. Below is the displaying the names of each of them. All of these classes are available in the “selenium.webdriver.support.expected_conditions” Python module.

  1. class alert_is_present
    It allows waiting for an alert to appear.
  2. class element_located_selection_state_to_be(ui_locator, is_selected)
    It allows waiting for an element to locate having a specified state.
  3. class element_located_to_be_selected(ui_locator)
    It allows waiting for an element to locate in the selected state.
  4. class element_selection_state_to_be(ui_element, is_selected)
    It allows waiting to find an element having a specified state.
  5. class element_to_be_clickable(ui_locator)
    It allows waiting for an element to locate which is in a clickable state.
  6. class element_to_be_selected(ui_element)
    It allows waiting for an element to find which is in a selected state.
  7. class frame_to_be_available_and_switch_to_it(ui_locator)
    It allows waiting for a frame to become available for the switchover.
  8. class invisibility_of_element_located(ui_locator)
    It allows waiting for an element which is invisible or not in the DOM.
  9. class staleness_of(ui_element)
    It allows waiting for an element getting removed from the DOM.
  10. class text_to_be_present_in_element(ui_locator, inner_text)
    It allows waiting for an element to find with a given text.
  11. class text_to_be_present_in_element_value(ui_locator, value)
    It allows waiting for an element to find with a given text inside the locator.
  12. class title_contains(title_text)
    It allows waiting to find a title containing the specified text in a case-sensitive format.
  13. class title_is(title)
    It allows waiting to find a title matching the exact specified text.
  14. class visibility_of(ui_element)
    It allows waiting to find an element which is functionally visible.
  15. class visibility_of_all_elements_located(ui_locator)
    It allows waiting to find all functionally visible elements the specified locator will return.
  16. class visibility_of_any_elements_located(ui_locator)
    It allows waiting to find at least one functionally visible elements the specified locator will return.
  17. class visibility_of_element_located(ui_locator)
    It allows waiting to find a specific and functionally visible element the specified locator will return.

In Selenium Python binding, you can easily find methods to handle these. It saves you from writing any user-defined expected condition class or creating a package for the same.

Quick Wrapup – Selenium Webdriver Waits in Python

Understanding of Selenium Webdriver waits is a key to produce high-quality automation test scripts. We hope this tutorial would have directed you to the right approach.

For more updates on Selenium Python tutorials, do follow our social media (Facebook/Twitter) accounts.

Best,

TechBeamers





Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here