2014-06-20 20 views
0

我通过webdriver找到元素来测试一个web应用程序,我想知道如果第一个元素没有找到,那么我想继续执行第二个代码,所以我放置了一个最后,但是用我的代码,即使在最终放置之后,代码也会停止执行,并且不会继续第二步。在哪里放置最后在java中的try catch块

请帮忙,谢谢 这里是我的代码:

// ---First Step----------------------------------------- 
try { 
    driver.findElement(By.className("label")).isDisplayed(); 
    System.out.println(" label is displayed"); 
} catch (NoSuchElementException e) { 
    System.out.println("label is not displayed"); 
} finally { 
    System.out.println("Go to the next step"); 
} 

driver.findElement(By.className("label")).click(); 

Thread.sleep(3000); 

// ---Second Step------------------------------------------ 
try { 
    driver.findElement(By.linkText("Resumé")).isDisplayed(); 
    System.out.println("Resumé is displayed"); 
} catch (NoSuchElementException e) { 
    System.out.println("Resumé is not displayed"); 
} finally { 
    System.out.println("Go to the next step"); 
} 

driver.findElement(By.linkText("Resumé")).click(); 
+2

你可以请包括你的程序的输出,所以我们可以看到它有多远? – clearlyspam23

+0

例如,如果程序没有找到webelement“label”,它应该显示消息“label is not displayed”,它将继续执行第二步。 – user3745193

+1

好的,但它在运行时实际显示的是什么?它显示“标签不显示?”它显示的是什么?如果没有,还有什么? – clearlyspam23

回答

0

在第一步点击标签,只有当元素存在。点击操作在try catch块之外。因此,移动两个点击行动在尝试

try { 
    driver.findElement(By.className("label")).isDisplayed(); 
    System.out.println(" label is displayed"); 
    driver.findElement(By.className("label")).click(); 
} catch (NoSuchElementException e) { 
    System.out.println("label is not displayed"); 
} **finally { 
    System.out.println("Go to the next step"); 
}** 

Thread.sleep(3000); 

//---Second Step------------------------------------------  
try { 
    driver.findElement(By.linkText("Resumé")).isDisplayed(); 
    System.out.println("Resumé is displayed"); 
    driver.findElement(By.linkText("Resumé")).click(); 
} catch (NoSuchElementException e) { 
    System.out.println("Resumé is not displayed"); 
} **finally { 
    System.out.println("Go to the next step"); 
}** 
+0

谢谢阿布舍克。 – user3745193