I cannot navigate to else part of my selenium code

2019-07-17 04:02发布

问题:

Please see my below code:

My if part of code is running successfully but my else part is not working. The code gets skipped and is showing failure.

 if (driver.findElement(By.id("error_explanation")).isDisplayed() == true) {
     driver.findElement(By.id("user_email")).clear();
     driver.findElement(By.id("user_email")).sendKeys("soumya50@toobler.com");
     driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
     driver.findElement(By.id("user_password")).sendKeys("password");
     driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
     driver.findElement(By.id("user_current_password")).sendKeys("password");
     driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
     driver.findElement(By.name("commit")).click();
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     driver.findElement(By.linkText("LOGOUT")).click();
 } else

 {
     driver.findElement(By.linkText("REQUEST A PERMIT")).click();
     driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
 }

Getting error as below:-

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"error_explanation"}

回答1:

You are getting the error because when the element is not visible... it is apparently not on the page at all. You first need to check to see if the element exists, then see if it's visible.

The proper way to check if an element exists according to the Selenium docs is to use .findElements() and check for an empty List. I combined the .isEmpty() check with the .isDisplayed() check in your existing if. This should work now.

List<WebElement> errorExplanation = driver.findElements(By.id("error_explanation"));
if (!errorExplanation.isEmpty() && errorExplanation.get(0).isDisplayed())
{
    driver.findElement(By.id("user_email")).clear();
    driver.findElement(By.id("user_email")).sendKeys("soumya50@toobler.com");
    driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
    driver.findElement(By.id("user_password")).sendKeys("password");
    driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
    driver.findElement(By.id("user_current_password")).sendKeys("password");
    driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
    driver.findElement(By.name("commit")).click();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.findElement(By.linkText("LOGOUT")).click();
}
else
{
    driver.findElement(By.linkText("REQUEST A PERMIT")).click();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}


回答2:

In Selenium you should not use driver.findElement to check if an element exists or not. Use driver.findElements instead. It returns a list of WebElement. You can then check if the list is empty or not.

Please view this for more information



回答3:

Use below code :-

1st way:-

public static void main(String[] args) {
    driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
    driver.get("URL");
    By element=By.id("error_explanation"));

    Boolean isPresent =isElementPresent(element);

    System.out.println(isPresent);

    if(isPresent==true)
    {
        System.out.println("yes");
    }
    else
    {
        System.out.println("No");
    }

}


public boolean isElementPresent(By locatorKey) {
    try {
        driver.findElement(locatorKey);
        return true;
    } catch (org.openqa.selenium.NoSuchElementException e) {
        return false;
    }
}

2nd way:-

    By element=By.id("error_explanation"));

    Boolean isPresent = driver.findElements(element).size() > 0;

    System.out.println(isPresent);

    if(isPresent==true)
    {
        System.out.println("yes");
    }
    else
    {
        System.out.println("No");
    }


回答4:

Create a new function as below,

public boolean findElementById(String id)
{
        try {
        webDriver.findElement(By.id(id));
    } catch (NoSuchElementException e) {
        return false;
    }
    return true;

}

check if condition as below in your code:

 if (findElementById("error_explanation")) {
  .....
 }else {
  .....
 }