Are We Using Proper Wait In Proper Way In Selenium Webdriver?

Hello Folks,

We will see a very basic concept of waits in selenium. I have already covered waits in selenium in details. You can go through below link:

Wait in Selenium webdriver

Someday ago, one guy asked me that explicit wait is not working for him. Webdriver is not using wait time to find web element and throw NoSuchElementException. Given wait time is completely neglected by webdriver.

He has written code as below:

Explicit wait code:

It was throwing below exception:
Exception in thread “main” org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element

He tried so many things like version upgrade, different browsers , operating system as well but same behavior. I solved his problem and I thought to write a post on this.

Do we understand wait before using it in Selenium webdriver?
If yes, you are excellent. If not, don’t worry, read this post.
Behavior of selenium webdriver is correct in above example. You are asking webdriver to wait till some web element is visible. Your are not instructing webdriver to wait till element is located and visible. In above wait condition (visibilityOf), webdriver expects that element is present in DOM and it will wait till its visibility. It will not wait if web element is not located. SO when webdriver does not locate webelement instantly, it throws NoSuchElementException in no time. It will not wait because wait time is set for visibility of web element not for locating web element.

We will see one example:
Java code:

Output:

You can see the timing. Webdriver didn’t wait to locate web element as we are not instructing him to wait if not located. So it throws NoSuchElementException because it didn’t find web element.

Now we will same example in positive way. In this program we will pass correct locator but web element will not be visible in browser display area.

Java Code:

Output:

You can see this time webdriver waits till maximum time for visibility of web element and throw TimeoutException on timeout. It try to find the web element and wait for its visibility.

I hope you will be clear with concept how webdriver works with explicit wait.
That’s it guys. If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning