0

私には機能があります-

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;
        }
}

アクションが可能な場合は関数が true を返すか、そうでない場合は何らかのメッセージを返し、次のステップに進みます。今、私はエラーが発生します -

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)

そして処刑はそこで止まります。

これを希望どおりに機能させるには、どうすればよいですか。

4

1 に答える 1

2

エラーではなく例外をキャッチしてみてください。問題なく動作するはずです。

エラーはjava.lang.Errorから派生し、例外はjava.lang.Exceptionから派生します。APIによると、エラーは「合理的なアプリケーションがキャッチしようとしてはならない深刻な問題を示しています」。例外は、「妥当なアプリケーションがキャッチしたい条件を示します」。

于 2012-09-25T12:37:47.827 に答える