waits in selenium webdriver

Exploring the wait commands in selenium webdriver

Are you using sleep commands in your Selenium Webdriver automation tests? While it may seem like an easy solution for pausing test execution, relying on sleep can actually harm the efficiency and reliability of your tests.

In this blog post, we’ll dive into the reasons why using sleep in Selenium is a bad idea and explore different types of wait for controlling the timing of your tests scripts.

Using fluent waits in Selenium allows us to properly wait until certain conditions of elements are met before actually trying to interact with them.

If you are learning Selenium Webdriver you probably faced the problem of having Selenium raise an exception telling you that an element that in fact is present on the screen, is not clickable, or visible.

In this article, I will explain how to create more robust tests using fluent waits in Selenium so you can wait for the proper expected conditions until trying to do something with an element.

Why using Sleep in Selenium is a bad idea?

The problem with using Thread.sleep is that we don’t know how much time to wait, so we have to guess. A real life example is when you are waiting for someone at a bar, you don’t know how much to wait, but when the person arrives you stop waiting.

Well, using sleep is like waiting for a fixed amount of time, for example, 45 minutes. If the person arrives after 15 minutes, we still wait 30 more minutes, and if the person arrives after 50 minutes, we stopped 5 minutes before.

So talking about web elements again, let’s say that we wait for 10 seconds for an element to appear, in that case we have 2 scenarios:

  • If the element appears in 6 seconds, we are still waiting 10 seconds, losing 4 valuable seconds in our execution.
  • If the element takes 11 seconds to appear, the test will fail after waiting 10 seconds.

So using dynamic and more intelligent waits we can have robust tests and save valuable execution time, something important if we are executing our test cases in a CI/CD environment.

Why do we need fluent waits in Selenium?

Fluent waits are a useful feature in Selenium that allows the test script to wait for an element to be available before interacting with it. This is important because web pages often load dynamically, which means that elements may not be immediately available when the page loads.

Without fluent waits, the test script might try to interact with an element that is not yet present, which could cause the test to fail. Fluent waits allow the script to wait for a specified amount of time for the element to become available before interacting with it, which can help to improve the reliability of the tests.

In addition to waiting for elements to become available, fluent waits can also be used to wait for elements to disappear or to wait for specific conditions to be met. This can be useful in situations where the web page is loading slowly or where elements are being loaded asynchronously.

Overall, fluent waits are an important tool in Selenium for helping to ensure that tests are reliable and can handle dynamic web page content.

Implicit wait in Selenium:

An implicit wait is when we just wait for a fixed amount of time before trying to interact with an element. This type of wait is declared in the following way:

Python implicit wait:

from selenium import webdriver
driver = webdriver.Firefox()
# Set the implicit wait for the driver object
driver.implicitly_wait(10)  # 10 seconds
# Navigate to a website
driver.get("https://www.example.com")
# Perform actions on the website
element = driver.find_element_by_id("some-element")
element.click()
# Close the browser
driver.quit()
  

In this example, an implicit wait of 10 seconds is set for the driver object. This means that if the driver cannot find an element immediately, it will keep trying for up to 10 seconds before throwing an exception.

This is not a good approach, mainly because it impacts the whole execution and now all the timeouts are hard coded to 10 Seconds.

Also, this does not guarantee that the element that appeared is ready to be used.

The default value for the implicit wait in selenium is 0 and I do not recommend modifying it, you should use another strategy.

Explicit wait in Selenium:

Python explicit wait:

Using Explicit waits allows you to stop the execution until a certain condition is met, for example, let’s say that we want to click on an element, in that case, we can wait until that specific element is visible, and continue the execution after the condition is met or stop after the timeout happens:

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.Firefox()
# Navigate to a website
driver.get("https://www.example.com")
# Use an explicit wait to wait for an element to be visible
element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.ID, "some-element"))
)
# Perform actions on the website
element.click()
# Close the browser
driver.quit()

In this example, a WebDriverWait object is created, and the until method is used to wait for an element to be visible. The visibility_of_element_located condition is passed as an argument to the until method. The WebDriverWait will keep checking for the visibility of the element for up to 10 seconds before throwing a TimeoutException if the element is not found.

Selenium expected conditions:

There are several expected conditions that we can use with this approach and we can use them by themselves or by mixing a group of conditions that needs to be met before interacting with an element.

  • alertIsPresent()
  • elementSelectionStateToBe()
  • elementToBeClickable()
  • elementToBeSelected()
  • frameToBeAvaliableAndSwitchToIt()
  • invisibilityOfTheElementLocated()
  • invisibilityOfElementWithText()
  • presenceOfAllElementsLocatedBy()
  • presenceOfElementLocated()
  • textToBePresentInElement()
  • textToBePresentInElementLocated()
  • textToBePresentInElementValue()
  • titleIs()
  • titleContains()
  • visibilityOf()
  • visibilityOfAllElements()
  • visibilityOfAllElementsLocatedBy()
  • visibilityOfElementLocated()

Selenium wait for page to load

When we are working with web pages we can expect different page load times, so is not rare that we get an exception when accessing elements just after we open the web page.

The solution for selenium wait for page load is is to configure the page load timeout in selenium so it will wait that amount of time before raising an exception.

from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference('webdriver.load.strategy', 'unstable')
driver = webdriver.Firefox(profile)
driver.set_page_load_timeout(5)

In the above example, selenium will wait up to 5 seconds for the page to be loaded.

Selenium wait for alert present

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.Firefox()
# Navigate to a website
driver.get("https://www.example.com")
# Use an explicit wait to wait for an alert to be present
WebDriverWait(driver, 10).until(EC.alert_is_present())
# Switch to the alert
alert = driver.switch_to.alert
# Perform actions on the alert, such as accepting or dismissing it
alert.accept()
# Close the browser
driver.quit()

In this example, a WebDriverWait is used to wait for an alert to be present on the page. The alert_is_present condition is passed as an argument to the until method.

The WebDriverWait will keep checking for the presence of the alert for up to 10 seconds before throwing a TimeoutException if the alert is not found.

The switch_to.alert method is used to switch the focus of the driver to the alert. This allows you to interact with the alert and perform actions such as accepting or dismissing it.

Selenium wait for element visible

Selenium wait for element to be visiblefrom 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.Firefox()
# Navigate to a website
driver.get("https://www.example.com")
# Use an explicit wait to wait for an element to be visible
element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "some-element")))
# Perform actions on the website
element.click()
# Close the browser
driver.quit()

In this example, an explicit wait is used to wait for an element to be visible on the page. The visibility_of_element_located condition is passed as an argument to the until method of a WebDriverWait object.

The WebDriverWait will keep checking for the visibility of the element for up to 10 seconds before throwing a TimeoutException if the element is not found.

The visibility_of_element_located condition checks whether the element is present in the DOM and whether it is visible to the user.

Once the element is visible, it is stored in a variable named element and can be interacted with in the rest of your script.

Selenium wait for text to be present on element

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.Firefox()
# Navigate to a website
driver.get("https://www.example.com")
# Use an explicit wait to wait for a specific text to be present in an element
element = WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element((By.ID, "some-element"), "Expected Text"))
# Perform actions on the website
element.click()
# Close the browser
driver.quit()

Here, an explicit wait is used to wait for a specific text to be present in an element on the page.

The text_to_be_present_in_element condition is passed as an argument to the until method of a WebDriverWait object.

The WebDriverWait will keep checking for the presence of the specified text in the element for up to 10 seconds before throwing a TimeoutException if the text is not found.

The text_to_be_present_in_element condition checks for the presence of the specified text in the element.

Once the text is present in the element, the element is stored in a variable named element and can be interacted with in the rest of your script.

Differences between Implicit and explicit wait commands

This is a great interview question that any level candidate should be able to answer in a tech interview: What are the types of selenium waits?

Implicit WaitExplicit Wait
Affects all webdriver elementsOnly affects specified elements
Don’t allow to specify expected conditionsRequires us to specify the ExpectedConditions on the element to be located
Not recommended at all, only for debugging purposes.Very effective for robust tests and for using best practices.
The default behaviourMust be defined and customized
Don’t support nested waitsWe can add nested waits
No support for element absence checkingAllows us to check for absence of an element
It runs on the serverIt runs locally

When to use selenium implicit wait in selenium webdriver

Implicit waits are a global setting that tells Selenium to wait for a specified amount of time before timing out when trying to find an element on the page. For example, if an implicit wait of 10 seconds is set, Selenium will wait for up to 10 seconds of polling time for the element to appear before timing out. This can be useful for pages that load slowly or where elements are being loaded asynchronously.

When to use selenium explicit wait in selenium webdriver

Explicit waits, on the other hand, are used to pause the test scripts for a specific condition to be met before continuing. This is different from an implicit wait, which simply waits for a specified amount of time before timing out.

For example, an explicit wait could be used to wait for an element to become clickable before clicking it. This can be useful when the element is being loaded dynamically and may not be immediately available when the page loads.

Overall, both implicit and explicit waits can be useful in Selenium test automation, depending on the specific needs of the tests. Implicit waits are useful for handling slow-loading pages or asynchronous loading of elements, while explicit waits are useful for waiting for specific conditions to be met before interacting with elements on the page.

Conclusion

In summary, fluent waits in selenium provide a lot of advantages and flexibility at the time of interacting with elements on the screen. These waits help us to achieve more robust tests by providing an easy way to verify the conditions that we need before performing an action on an element.

1 thought on “Exploring the wait commands in selenium webdriver”

  1. Pingback: Difference between implicit and explicit wait in selenium - Relevant Codes

Leave a Comment

Scroll to Top