How to bypass the implicit wait condition while looking for webelement which is not present on UI?

162 Views Asked by At

I am automating a scenario wherein a specific web element might or might not be displayed on the UI. If it is getting displayed, then I want to perform a specific action on it. I am using below logic for the same

try{
if(element.isDisplayed())
    {
     //perform action on the element if it is visible
    }
}
catch(Exception e)
    {
    }

The code works fine whenever the element is visible on the UI. But during scenarios when the element is not displayed, then 'element.isDisplayed()' waits for the element for 10 seconds (i.e the implicit wait time which I have defined for the driver session).

I want my script to not wait for that 10 seconds for the element to appear, instead just go ahead with further actions. Any idea what approach should I go with here?

1

There are 1 best solutions below

0
On

It seem like you can achieve with decrease timeout value utilize WebDriverWait.

Try to create a boolean function to check the element presence or not with specific time.

public boolean checkElement(By locator, int seconds) {
    boolean find = false;
    try {
        new WebDriverWait(driver, seconds).until(ExpectedConditions.presenceOfElementLocated(locator));
        find = true;
    } catch (Exception e) {
        // TODO: handle exception
    }
    return find;
}

Just call with:

By locator = By.name("yourLocator");

if(checkElement(locator, 1)) {
    //perform here
    ....
}

The following imports:

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

Please delete implicitWait which you've declared before.