Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to catch exception NoSuchElementException

My try block throws NoSuchElementException but my catch block is unable to proceed.

In my automation suite I get a page with btnOk element sometimes(first login everyday) so I am trying to handle the scenario where if the page comes then click on it and proceed otherwise continue any ways.

Code snippet below:

try {
    WebElement submitbuttonPresence=driver.findElement(By.id("btnOk"));
    submitbuttonPresence.click();
}
catch (NoSuchElementException e) {
    System.out.println(driver.getTitle());
}
like image 369
Ravi Avatar asked Dec 18 '22 17:12

Ravi


1 Answers

It seems you catch an incorrect exception. Try code below:

try {
    WebElement submitbuttonPresence=driver.findElement(By.id("btnOk"));
    submitbuttonPresence.click();
}
catch (org.openqa.selenium.NoSuchElementException e) {
    System.out.println(driver.getTitle());
}
like image 107
Buaban Avatar answered Jan 06 '23 02:01

Buaban