考虑:
if(driver.findElement(By.xpath("//*[@id='myClass']/tr[1]")) != null){
// Passed
}
else{
// Failed
}
我应该写什么才能让 Selenium 通过或失败?我可以System.out.println("failed");
,但它仍然显示Failure = 0
最后。
最佳答案
使用findElements
而不是findElement
并检查大小
:
if(driver.findElements(By.xpath("//*[@id='myClass']/tr[1]")).size() != 0){
//passed
}else{
//failed
}
您还可以捕获异常:
try {
WebElement element = driver.findElement(By.xpath("//*[@id='myClass']/tr[1]"));
//passed
} catch (NoSuchElementException ex) {
//failed
}
关于java - Selenium 最后有条件失败或通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37585627/