使用Selenium WebDriver页面工厂检查元素是否存在?

时间:2013-07-29 20:15:08

标签: selenium selenium-webdriver

使用普通的WebDriver方法检查 findElements 的元素很简单:

boolean exists = driver.findElements( By.id("...") ).size() != 0

Page Factory 初始化的元素如下:

@FindBy(name = "filter")
private WebElement filterText;

但是我们如何在页面中检查该元素是否存在于页面上?

2 个答案:

答案 0 :(得分:0)

isDisplayed()方法应该完成这项工作:

if (filterText.isDisplayed()) {
    filterText.doStuff();
}

答案 1 :(得分:0)

这是我提出的事情:

public boolean isElementPresent(WebElement we)
    {  
        try {

            we.getTagName();

        } catch (NoSuchElementException e) {
            flag = 1;
        }   
       if (flag == 1)
            return true;  
       else
           return false;
    }

这是非常基本但有效的方法..