我通过使用webdriver查找元素来测试Web应用程序,我想知道是否找不到第一个元素然后我想继续执行第二个代码,所以我放了一个finally但是我的代码,甚至之后最后放置,代码停止执行,但没有继续第二步。
请帮忙,谢谢 这是我的代码:
// ---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();
答案 0 :(得分:0)
在第一步中仅在元素存在时单击标签。点击操作在try catch块之外。所以在try
中移动了两个点击操作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");
}**