我在 Webdriver 中定义了 Boolean
变量,支持如下:
boolean r1 = selenium.isTextPresent("something");
我在 while
循环中使用了它。我想将我的代码转换为 Webdriver 代码,我尝试过:
boolean jr1 = driver.findElement(By.linkText("something")) != null;
但它仅在存在文本且值为 true
时才有效。当值应该作为 false
返回到我的变量时,我在控制台中收到错误:
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"link text","selector":"something"}
您能给我一些建议吗?
最佳答案
我通过编写返回 true 或 false 的 isElementPresent 方法解决了这个问题。 编辑-刚刚在另一个答案中看到了这个确切的方法,我不再相信我写了它。显然我很久以前就发现了它并且已经使用它很长时间了,感觉就像我的一样!一切归功于原作者
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
然后您可以使用:
boolean jr1 = isElementPresent(By.linkText("Something"));
将此方法添加到基类并从中扩展您的测试类,或者将其添加到每个测试类。
关于selenium - Webdriver 中的 boolean 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16418312/