DEV Community


intro to waits

Tony Colston
wanna be gamedev ... all around nerd and dev
・2 min read

In most simple example scripts I have written for I have not included any Waits. I am going to introduce the topic here.

The concept is easy.

You as a human when testing a website will click on something and then you wait until the page is "done." What does done mean for a web page?

Ultimately the browser knows when the page is loaded. There is an event you can catch in JS called window.onload.

How does a Selenium program do it? With code if course.

There is a concept of a Wait that matches up to your intuition. It is a programming/code concept that matches that human ability to wait for something to happen.

There are two kinds of Waits in Selenium.

Explicit and Implicit.

Implicit waits are for the entire script. You set one number usually at the start of the Selenium script. I want to Wait for 10 seconds for anything in this script. That is the maximum I will wait for any one command to finish. And that implicit wait applies across the entire script.

For some scripts the implicit wait is enough you can set it and forget it. And if some operation in your script is slow your script will fail.

The Explicit Wait is more fine grained (or more explicit). You can as a programmer say click on this button and then wait up to 15 seconds for a result. Or wait up 10 seconds for the word "monkey" to appear in the title. Or wait up 11 seconds for the 3rd div from the top of the screen to be orange... just about anything you can think of on a web page. You can write an Explicit Wait to handle it.

Here are some of those waits

  • title contains a word
  • an element becomes visible
  • text in an element
  • and many others...

This post has been about the two main concepts needed for Waits in Selenium. In another post I will show what Waits will look like in code.

Discussion (0)