HowTo Wait

Ondřej Machulda edited this page Apr 3, 2017 · 12 revisions

Explicit Waits

An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with WebDriverExpectedCondition is one way this can be accomplished.

// Wait for the page tile to be 'My Page'. 

// Default wait (= 30 sec)
  WebDriverExpectedCondition::titleIs('My Page')

// Wait for at most 10s and retry every 500ms if it the title is not correct.
$driver->wait(10, 500)->until(
  WebDriverExpectedCondition::titleIs('My Page')

Available conditions to wait for

There are many prepared conditions you can pass to the until() method. All of them subclass WebDriverExpectedCondition.

Wait for title

  • titleIs()
  • titleContains()
  • titleMatches()

Wait for URL

  • urlIs()
  • urlContains()
  • urlMatches()

Elements and their text

  • presenceOfElementLocated()
  • presenceOfAllElementsLocatedBy()
  • elementTextIs()
  • elementTextContains()
  • elementTextMatches()
  • textToBePresentInElementValue()

Elements visibility and invisibility

  • visibilityOfElementLocated()
$driver->wait(10, 1000)->until(
  • visibilityOf() - note the element must already be present in the DOM and you only waits until it became visible
$element = $driver->findElement(WebDriverBy::id('first_name'));
$driver->wait(10, 1000)->until(
  • invisibilityOfElementLocated()
  • invisibilityOfElementWithText()

Frames, windows, alerts

  • frameToBeAvailableAndSwitchToIt()
  • elementToBeClickable()
  • alertIsPresent()
  • numberOfWindowsToBe()

Special conditions

  • stalenessOf()
  • refreshed()
  • not()
  • elementToBeSelected()
  • elementSelectionStateToBe()

Custom conditions

The until() method accepts also any callable, providing you an possibility to implement custom condition like this:

    function () use ($driver) {
        $elements = $driver->findElements(WebDriverBy::cssSelector(''));

        return count($elements) > 5;
    'Error locating more than five elements'

This example condition will wait until more than 5 elements with selector appears.

Implicit Waits

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.


It is recommended to use explicit wait instead of the implicit one. See for example this Stackoverflow answer for some reasoning.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.