A very hot topic when testing with Selenium is how to wait for a WebElement to be displayed. I wrote about this some while back, and that post is one of my most read on this blog. In this new post i will revisit the subject, by providing a new version of that method, using Java 8. It can be found in ‘thewaiter’ library in two variants: with a default timeout, and with a signature that allows a timeout parameter to be specified when calling it.

What these methods do:

  • during the timeout period (which by default, if not explicitly set, is 30 seconds), every half second the condition is checked (whether the element is displayed). The check is done by calling the ‘isDisplayed()’ method from Selenium.
  • if the element is displayed within the allocated timeout period, the wait method exits successfully and the rest of the test is run
  • if the element is not displayed, and the timeout period elapsed, a TimeoutException is thrown

It’s worth to mention how the ‘isDisplayed’ method works, the one used for determining whether the WebElement is displayed, inside the wait method. I will refer to what happens when you use the wait methods with a WebElement defined in a PageObject class (as i mentioned, this is a must when using these waits; driver.findElement will not work with the waits). So, when calling the wait method on the WebElement, when the ‘isDisplayed’ line of code is executed:

  • if the WebElement is not present in the HTML of the page at all, or if it is present but it has some attribute that indicates it is hidden, the wait method will try to locate the element again (every half second)
  • only if the WebElement is present in the HTML and it has no “hidden” attribute, will the wait method exit successfully
The wait methods: waitForElementToBeDisplayed

So what do the two waitForElementToBeDisplayed methods look like? First, the variant which requires the timeout parameter to be sent when calling it:

public void waitForElementToBeDisplayed(WebElement element, WebDriver driver, int specifiedTimeout) {
WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout);
ExpectedCondition<Boolean> elementIsDisplayed = arg0 -> element.isDisplayed();

The method variant that uses the default timeout period declared in ‘thewaiter’ library, based on the previous one:

public void waitForElementToBeDisplayed(WebElement element, WebDriver driver) {
waitForElementToBeDisplayed(element, driver, TIMEOUT);

To exemplify the wait methods, let’s consider the following use case: you open a page, click on the search icon and the search field is displayed. At this point, all of these actions will be done by using wait methods, to have a reliable test: open the URL and wait for the page to load completely, click the search icon, and wait for the search field to be displayed.

All the WebElements are declared in the Page Object class of the test. For this example, they look like:

@FindBy(how = CSS, using = ".search-toggle")
public WebElement searchButton;
@FindBy(how = CSS, using = ".search-field") public WebElement searchField;

Here is the variant that uses the default timeout value for the element to be displayed:

waiter.get("specifyTheUrlHere", driver);
waiter.click(page.searchButton, driver);
waiter.waitForElementToBeDisplayed(page.searchField, driver);

And here is the variant of the same test, in which the timeout for the element to be displayed is specified in the test as being 10:

waiter.get("https://imalittletester.com", driver);
waiter.click(page.searchButton, driver);
waiter.waitForElementToBeDisplayed(page.searchField, driver, 10);

Where to find the code and examples

The GitHub repo of thewaiter library, with a “how to use” documentation:


The downloadable release version of thewaiter:


The library’s javadoc:


The tests that use the methods from this post:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.