使用普通的 WebDriver 方法使用findElements检查元素很容易,例如:
boolean exists = driver.findElements( By.id("...") ).size() != 0
页面工厂初始化的元素如下:
@FindBy(name = "filter")
private WebElement filterText;
但是我们如何在我们的页面中检查该元素是否存在于页面上?
使用普通的 WebDriver 方法使用findElements检查元素很容易,例如:
boolean exists = driver.findElements( By.id("...") ).size() != 0
页面工厂初始化的元素如下:
@FindBy(name = "filter")
private WebElement filterText;
但是我们如何在我们的页面中检查该元素是否存在于页面上?
这是我想出的:
public boolean isElementPresent(WebElement we)
{
try {
we.getTagName();
} catch (NoSuchElementException e) {
flag = 1;
}
if (flag == 1)
return true;
else
return false;
}
这是非常基本但有效的方法..
isDisplayed()方法应该可以完成这项工作:
if (filterText.isDisplayed()) {
filterText.doStuff();
}