mercredi 1 août 2018

Selenium Webdriver (JAVA) code exiting on "If condition" on no existence of element with NoSuchElementException.

I'm trying to automate a script. You can get the idea of the code as below.

driver.findElement(By.id("ctl00_body_txtUserID")).sendKeys("Donhere");
driver.findElement(By.id("ctl00_body_txtPassword")).sendKeys("don@123");
driver.findElement(By.id("ctl00_body_btnLogin")).click();
if(driver.findElement(By.className("tcuarea")).isDisplayed()){
     driver.findElement(By.id("ctl00_Add")).click();
}
driver.findElement(By.id("ctl00_Add_to_Sum")).click();

The element with id ctl00_Add shows up rarely. So when if condition results in TRUE, the script continues well. But on FALSE condition, it still enters to if condition, when no such element is displayed. It results in NoSuchElementException exception.

Why doesn't the script move directly to next step?

Any solution without try, catch method?

Aucun commentaire:

Enregistrer un commentaire