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:


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:


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.

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “Are We Using Proper Wait In Proper Way In Selenium Webdriver?

  1. Hi Amod,
    Can you please try below code & let me know if it executes at your end:
    package facebooklogincontrolverify;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import java.util.*;
    import java.util.concurrent.*;

    public class Randomvalueselection {

    public static void main(String[] args) {
    System.setProperty(“”, “C:/Selenium drivers/geckodriver-v0.19.0-win64/chromedriver.exe”);;
    WebDriver driver=new ChromeDriver();
    WebElement dropdown=driver.findElement(By.xpath(“//div[text()=’File’]”));
    for(int i=0;i<4;i++)
    WebDriverWait wait=new WebDriverWait(driver,15);
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[@class='menu transition visible']"))));
    List item=(List) driver.findElement(By.xpath(“//div[@class=’menu transition visible’]//div[@class=’item’]”));
    int size=item.size();
    int random=ThreadLocalRandom.current().nextInt(0, size);
    System.out.println(“Random value”+random);




    At my end it throws NosuchElement exception as the web element is not visible.I have passed the appropriate xpath.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.