Assert the presence of a web element (with a wait)


I had an issue where I was checking to see if an element exists but the element was not fully loaded by the time the code did the check, so it would fail. I could put a sleep in the code to give the element time to load but that is not a good solution since a 2 second sleep today could need a 3 or 4 second sleep tomorrow depending on server and network load. I wanted to keep the code related to the web element in the page object file but the driver.findElements function does not provide a wait so I came up with this solution.

I posted this as a Help topic to see if there is a better way to do this.

In “my page object file”, I created a method that will look for “someElement” using the web driver wait value and returns true if it finds the element and false if it does not find it. I had to check to see if the result is not null since it returns an element object and cannot be converted to a Boolean for a basic if statement check.

   public Boolean isSomeElementPresent() {
      if (this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.someElement)) != null) {
         return true;
      return false;

Then in my test file I did a simple assert:

   Assert.assertTrue("my page object file".isSomeElementPresent());

FUTURE REQUIREMENT: A possible solution could be an addition to the list of “Is” options in the drop down when you drag an element into the page object file. You could add 4 options to the list by copying the existing ones and add “with Wait”. One might argue why not just always use the one “with wait” but I think there would be cases where you might choose a different option depending on the structure and speed of your application.


Hi @jmsc7ran
As you said, there are many use cases regarding this, and adding 4 more options to the context menu may clutter it. I will inform the team about your request, and see what could be done in this case :slight_smile:


Waiting for an elements presence or visibility is definitely one of the most important part when automating with Selenium and also the reason, if omitted, for a lot of flaky tests.
I am not sure I fully understand what your use case was but wouldn’t the Is -> element visible or is -> element present conditions do exactly what you’ve been looking for? The resulting code would be

// visible check
Boolean isVisible = this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.yourElement)).isDisplayed();

// presence check
Boolean isPresent = driver.findElements(this.yourElement).size() > 0;

By dragging this as a method and return the variable you should have your getter action available.

Moreover pretty much every action like Click or the like already do wait for the elements visibility


That said you should never have to actually use driver.findElement/s but instead always go the route through a wait + Expected Condition since it will result in your element just as findElement and on top of that perform the explicit wait until the condition is met.


Apologies, I did not fully understand how Is -> element visible works until now. I had used Is -> element present but the element was not fully loaded by the time I checked for it in the code. Once I changed it to ‘Is -> element visible’, it waited until the element was there and then continued with the code. It is working perfect now. Thank you!

The use case is I am checking web pages that have new elements added and want to ensure they are now present.


Alright that clears it up. Yep so checking for visibility is a long-lasting (you see that by the usage of this.wait) event. Is present is more of a one-off check which you’d do e.g right before an assertion where you know that the state of your SUT should be stabilized already.

closed #6