简体   繁体   中英

How to Deal with ElementNotFound Exception in Selenium WebDriver

Overview : I have already prepared the automation script in Selenium Web driver script in Java which will login into a website and make the selections automatically and once the selections are completed it will run the report.

What I want : I am facing issue while optimizing my automation script.

Brief Explanation : Actually I am familiar with different kinds of wait we are using in Selenium but all those wait ie implicit,explicit or fluent wait didn't able to help me out in making the code more optimized.Currently I am using Thread.sleep() method everywhere in order to run the script completely without any fail but I know this should not be best practice to be get followed because sometime Elements loads fast and sometime slow because of that either my Script execution took long time or failed based on Element availability.I created one separate method for Webdriver wait and Which I can call for various webelements whenever I needed in my main script but this approach also sometime works or sometime not even though I am passing 800 Second as Time period to wait but if I use Thread.sleep(5000) it will work without any issue not sure why ??

What I want to be have a separate method for wait which can be called in main script whenever required and I want my script to be worked flawless the moment webeelment visible just like what we human do when we interacting with any web.

Note : I have tried ExpectedCondition methods like elementtobeclickable, visibilityOfElementLocated,presenceofElementLocated all of them sometime these work but sometime won't.

Separate Method of wait I have created

public static WebElement waiting(WebDriver driver,String path){
    WebDriverWait wait = new WebDriverWait(driver,800);
    WebElement element=wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(path)));
    return element;
   }

Piece of main code where I am calling this Method.

if(nam.equals("Some name"))
{
WebElement e=driver.findElement(By.xpath("1st Webelement path"));
e.click();
System.out.println("Value clicked under First Drop Down is:"+e);
Listing.waiting(driver,"2nd WebElement xpath").click();
//Thread.sleep(5000);
//driver.findElement(By.xpath("2nd WebElement xpath")).click();
System.out.println("Second Dropdown clicked");

}

When I commenting the Thread.sleep() then it will throw the ElementNotFound exception even though I have used 800 Seconds in Webdriver wait method but the moment I removed the comment from Thread.sleep() method it will work.

Kindly Help me in getting the reusable and useful wait method which I can call several times in my main code.

Thanks in Advance !!

This usually works for me (not FluentWait though):

WebDriverWait wait = new WebDriverWait(driver, TIMEOUT);
    ExpectedCondition elementIsDisplayed = new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver arg0) {
      try {
         webElement.isDisplayed();
         return true;
      }
      catch (NoSuchElementException e ) {
        return false;
      }
      catch (StaleElementReferenceException f) {
        return false;
      }
        } 
    };
    wait.until(elementIsDisplayed);

Of course, setup TIMEOUT with the amount of time you want to wait for the element to be found (that is in seconds).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM