如何捕获多个webdriver异常时的OR

时间:2014-02-12 07:12:46

标签: java selenium selenium-webdriver

我正在尝试捕获这样的多个异常但我收到错误'); expected'。如何使用||

进行操作
   try {
         //find an element here
    }catch( StaleElementReferenceException e || NoSuchElementException e) {
        //do something            
    }

2 个答案:

答案 0 :(得分:5)

假设您使用的是Java 7,您应该可以使用以下语法:

catch (StaleElementReferenceException | NoSuchElementException e)

请注意 |以及单变量名称。

有关详细信息,请参阅"Catching Multiple Exception Types And Rethrowing Exceptions With Improved Type Checking" doc(引人注目的标题,是吗?)。

如果您不使用Java 7,则需要多个catch块。

答案 1 :(得分:0)

试试这个:

try {
  //find an element here
 }catch( StaleElementReferenceException e){
  // do something    
 }catch(NoSuchElementException e) {
  //do something            
 }

注意:第二个catch块必须捕获更广泛或更新的异常,否则代码将无法编译。