Selenium implicit and explicit wait, timeout exception element not found

4.1k Views Asked by At

I am new to selenium (but experienced java developer).

I am using something like below:

WebElement searchBasket = pDriver.findElement(By.xpath("//a[contains(.,'Search&Baskets')]"));
WebElement searchproduct = pDriver.findElement(By.xpath("//a[contains(.,'Search a product')]"));

//if search an agreement is not show up, then click on other menu, then click it back
pWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Search&Baskets')]")));
pDriver.findElement(By.xpath("//a[contains(.,'Search&Baskets')]")).click();

// click on search an agreement
try {
    pWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Search&Baskets')]")));
    action = new Actions(pDriver);
    action.moveToElement(searchBasket).build().perform();

    pWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Search a product')]")));
    searchproduct.click();
} catch (TimeoutException e) {
}

where pWait is:

WebDriverWait wait = new WebDriverWait(driver, 15);

however when I run the test case I get below error:

Unable to locate element: {"method":"xpath","selector":"//a[contains(.,'Search&Baskets')]"}
Command duration or timeout: 4 milliseconds

I thought it should have wait atleast 15 seconds before throwing this exception. From the log above it looks like it threw exception only in 4ms. and i could see on console that as soon as it hit that line, it threw exception.

I have implicit waiting set as 0 and using explicit wait.

Am i missing anything here.

Also, in explicit and implicit wait, is it upto that much time OR exact that much time, example if I set implicit wait as 10 sec, then does it mean wait for exact 10 sec OR wait upto 10 sec (if element found then proceed, even if element founf on 6th second)

is above same for explicit wait as well?

Please help

2

There are 2 best solutions below

0
On

ExpectedConditions.elementToBeClickable invokes isDisplayed() and isEnabled() methods on EXISTING WebElement.

You are providing By as a parameter which means, the driver has to find your element first. It failed to do this.

Make sure your element is present by using wait until presenceOfElementLocatedBy(By by).

Example:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocatedBy(By.xpath("//a[contains(.,'Search&Baskets')]")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Search&Baskets')]")));
0
On

Let us analyze what is happening in our code.

We have defined two WebElements searchBasket and searchproduct as follows :

WebElement searchBasket = pDriver.findElement(By.xpath("//a[contains(.,'Search&Baskets')]"));
WebElement searchproduct = pDriver.findElement(By.xpath("//a[contains(.,'Search a product')]"));

We havn't tried to use those WebElements in our code at immediate basis, so had No Impact.

Next, we tried WebDriverWait for an WebElement as follows :

pWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Search&Baskets')]")));

Again we didn't capture the return type of the result, so had No Impact.

Now, within the try{} block we have again tried WebDriverWait:

pWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Search&Baskets')]")));

But again we havn't captured/acted on the return type of the result. That's why moving ahead when we did :

action.moveToElement(searchBasket).build().perform();

searchBasket referred to the WebElement which we have stored earlier as :

WebElement searchBasket = pDriver.findElement(By.xpath("//a[contains(.,'Search&Baskets')]"));

As this first search result (which was without WebDriverWait) may not have returned any WebElement at all and have returned Null.

Finally, the most important factor for the error Unable to locate element: {"method":"xpath","selector":"//a[contains(.,'Search&Baskets')]"} is, the WebDriverWait instance was wait. Instead of using wait we have always tried to use pWait

So for all these reasons, WebDriverWait was never implemented properly in our code.


Mixing up ImplicitWait & ExplicitWait

The Selenium Documentation clearly mentions the following :

WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.