Waiting for a custom condition in Selenium or Appium

Nathan Krishnan
4 min readMar 1, 2022

--

Inspired by a discussion on our Discord server, I’d like to walkthrough how you can create custom wait conditions in your Selenium or Appium tests. This is a helpful technique to know, as you may encounter a situation in your test where having a single expected condition is not logically exhaustive enough to proceed. For example, if you need to wait for multiple elements to be present. I’ll explain and demonstrate this technique using the Selenium Python client.

Discussing interesting topics in automation testing is precisely why we created our Discord server! If you find this blog post helpful, or learned something new, consider joining our community today 👋:

Contents of this Guide

  • The until method of the WebDriverWait class
  • Creating callable Python classes
  • Example: Waiting for at least one of many elements

The until method of the WebDriverWait class

Let’s first start with taking a look at how the until method of the WebDriverWait class is implemented:

https://github.com/SeleniumHQ/selenium/blob/trunk/py/selenium/webdriver/support/wait.py#L63

The until method accepts a method and will invoke that method by passing in the driver object as an argument. If the method returns True then until can return, otherwise an exception will be raised. We can use this knowledge to create our own custom method that will check for our particular desired condition. That said, there’s an interesting nuance to how we can represent this method. In Python, classes can implement the __call__ magic method to give them the ability to behave like a function. This is a common practice with creating custom wait conditions and I’ll further explain this concept in the next section.

Creating callable Python classes

__call__ is a special magic method in Python that, when implemented in a class, gives instances of the class to ability to behave like a function. When implemented, the __call__ method will be invoked when the class is invoked. For example:

Example of a callable class

In this example, when the instance of the Car class is called its name and uniquely generated manufacturer ID will be printed to the output. For instance:

Car Name: Model 3 and Manufacturer ID: 2b62da0285894603a02b2a464b703d9d

Many Python packages, like the Python Selenium client, use the callable class paradigm as an interface for using the APIs provided. It provides the flexibility of organizing logic in a class, with the ease of invoking the logic like a function call.

Now that we understand how to create a callable class, let’s create one to express a custom wait condition in a Selenium test.

Example: Waiting for at least one of many elements

There may be a situation in your automation test where you would want to wait for at least one of many possible elements to be present before proceeding. This is a great use case for creating a custom wait condition. We can create a callable class that is initialized with a list of expected conditions:

Custom wait condition

If at least one of the expected conditions from the list occurs we can return True or otherwise False.

To utilize, we can create a list of conditions in our test case, pass it to our callable class, which we can provide as input to the until method of the WebDriverWait class. For example:

Using our custom wait condition

The End

That’s it we’re done! 🎉 You now have the prerequisite knowledge to create custom wait conditions in your Selenium and Appium tests.

Separately, I’m currently working on a platform that enables developers to efficiently diagnose, resolve, and optimize their automation tests. We currently have an ongoing beta program for users to try it and if you want an invitation you can join the waitlist here. Just click on “Request an Invite.”

--

--

Nathan Krishnan
Nathan Krishnan

Written by Nathan Krishnan

I enjoy writing about building & testing apps. Co-founder and CTO of Tauk (acquired by Moloco). https://www.nathankrishnan.com