Reputation: 3394
I have a function -
private boolean selectFromDropDown(String locator, String value) {
try {
new Select(driver.findElement(By.xpath(locator))).selectByVisibleText(value);
return true;
}
catch (Error e) {
verificationErrors.append(e.toString());
System.out.println("Could not find element");
return false;
}
}
I want the function to return true when the action is possible or else return some message and continue to the next step. Now I get an error -
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Cannot locate element with text: Indi
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.24.1', revision: '17205', time: '2012-06-19 16:53:24'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_07'
Driver info: driver.version: unknown
at org.openqa.selenium.support.ui.Select.selectByVisibleText(Select.java:147)
at com.adobe.auto.testcases.utils.SeleniumTest.selectFromDropDown(SeleniumTest.java:89)
at com.adobe.auto.testcases.utils.SeleniumTest.RunSeleniumTest(SeleniumTest.java:66)
at com.adobe.auto.testcases.utils.Excel_Reader.runTest(Excel_Reader.java:653)
at com.adobe.auto.testcases.utils.DriverFinal.main(DriverFinal.java:25)
and the executions stops there.
What do I need to do to make this working as I want.
Upvotes: 0
Views: 993
Reputation: 2021
Try catching rather Exceptions instead of Errors and it should work just fine.
Errors are derived from java.lang.Error, and Exceptions are derived from java.lang.Exception. According to the API An Error "indicates serious problems that a reasonable application should not try to catch." An Exception "indicates conditions that a reasonable application might want to catch."
Upvotes: 2