wait

The command to execute a method or function as a condition to wait for.

There are two types of Wait objects:

  • WebDriverWait (default)

    • Directly from Selenium

    • Returns WebElement and List[WebElement]

  • PyleniumWait

    • Returns Element and Elements

    • Has a built-in .sleep() method

wait.until(condition) is the most common use of Wait and allows you to wait until the condition returns a non-False value.

However, both Waits require the condition to use a WebDriver. In the example below, we can pass in a lambda (aka anonymous function) where x is the WebDriver.

# .is_displayed() returns a bool, so the return value is True
py.wait().until(lambda x: x.find_element(By.ID, "foo").is_displayed())
# the WebElement is returned once the element is found in the DOM
py.wait().until(lambda x: x.find_element(By.ID, "foo"))
# because use_py=True, this will now return Element instead
# also, this will wait up to 5 seconds instead of the default in pylenium.json
py.wait(5, use_py=True).until(lambda x: x.find_element(By.ID, "foo"))

Syntax

Usage

The usages are almost identical between the Wait objects, but you need to identify why you need to use a Wait in the first place. Pylenium does a lot of waiting for you automatically, but not for everything.

Remember, the biggest difference is what is returned: WebElement vs Element

WebDriverWait

This is the default Wait object. This will return WebElement, so you won't have Pylenium's Element commands like .hover() - that is what PyleniumWait is for.

  • Using the defaults

  • Using custom timeout

  • Using ignored_exceptions

By default, the only exception that is ignored is the NoSuchElementException. You can change this by adding a list of Exceptions that you want your condition to ignore.

  • Combine arguments

PyleniumWait

If you want to return Pylenium objects like Element and Elements, then set use_py=True. Otherwise, it works the same way as WebDriverWait.

  • PyleniumWait also includes a .sleep() command

Expected Conditions

Expected Conditions are a list of pre-built conditions that you can use in your Waits and can be used in either WebDriverWait or PyleniumWait to replace the lambda functions in the examples above.

Yields

  • Any - Whatever the non-False value of the condition is

Raises

  • TimeoutException if the condition is not met within the timeout time

  • Depending on the condition, it would raise other Exceptions. If you know which ones are expected, you can include them in the ignored_exceptions as an argument.

Last updated