2

This code is working fine if saleIdValueIs is correct but same is not working when saleIdValueIs data is not correct. Then it's showing a error message " org.openqa.selenium.NoSuchElementException: Unable to locate element"

        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//html/body/div/div/div[2]/div[2]/div/div/div/div/div/div/div/div/div[2]/div/div/div/div/div/fieldset/div/div/div/div[2]/div[2]/input")).sendKeys(saleIdValueIs);
        search_transaction_bt.click();
        boolean saleIdVisible=driver.findElementByXPath("/html/body/div/div/div[2]/div[2]/div/div/div/div/div/div/div/div/div/div/div").isDisplayed();
        String searchedSaleIdValue=saleIdValue.getText();
        System.out.println(saleIdVisible);
            if (saleIdVisible==true){
                    System.out.println("sale id  is  - "+saleIdValueIs+ "output is "+searchedSaleIdValue);
                    }       
                else{
                     System.out.println("error message is coming ");
                    boolean errorMessageDisplayTest= driver.findElement(By.xpath("/html/body/div[2]/div/div")).isDisplayed();
                    System.out.println(errorMessageDisplayTest);
                    boolean errorMessageVisible= driver.findElement(By.xpath("/html/body/div[2]/div/div")).isDisplayed();
                    System.out.println(errorMessageVisible);
                    driver.close();
                    }
1
  • Try to avoid such long xpaths,this might cause your test case to fail more often...you can use id,classname,name,title or other attributes to reduce the length of xpath. Much better results can be obtained with cssSelectors.
    – Amith
    Dec 12, 2013 at 8:32

1 Answer 1

3

Your getting that error because element
driver.findElementByXPath("/html/body/div/div/div[2]/div[2]/div/div/div/div/div/div/div/div/div/div/div") is not displayed,
So instead of using isDisplayed, please use isElementPresent. Below is code, let me in case of any queries

boolean saleIdVisible= isElementPresent(By.xpath("/html/body/div/div/div[2]/div[2]/div/div/div/div/div/div/div/div/div/div/div"), driver);
if(Present)
{
System.out.println("sale id  is  - "+saleIdValueIs+ "output is "+searchedSaleIdValue);
 else{
System.out.println("error message is coming ");
boolean errorMessageDisplayTest= driver.findElement(By.xpath("/html/body/div[2]/div/div")).isDisplayed();
System.out.println(errorMessageDisplayTest);
boolean errorMessageVisible= driver.findElement(By.xpath("/html/body/div[2]/div/div")).isDisplayed();
System.out.println(errorMessageVisible);
driver.close();
}


public static boolean isElementPresent(By by, WebDriver driver) 
    {
      boolean present;
      try
        {
          driver.findElement(by);
          present = true;
        }catch (NoSuchElementException e)
        {
          present = false;
         }
     return present;
    }
2
  • Isn't it too expensive in terms of performance to handle this by catching exception? Is it the only way to control this? Jul 8, 2015 at 20:23
  • @FelipeCorrea I wouldn't know the answer to that, but there's an alternative by checking the count using FindElements which won't throw an exception: if( driver.FindElements(By.CssSelector(exportCssSelector)).Count() > 0) {//element exists}
    – zeta
    Aug 31, 2016 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.