WebDriver:如何检查页面对象Web元素是否存在? [英] WebDriver: How to check if an page object web element exists?
本文介绍了WebDriver:如何检查页面对象Web元素是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当使用带有webdriver的页面对象时,如何检查元素是否存在。
How to check if an Element exists, when using Page Objects with webdriver.
到目前为止,我这样做。
So far I am doing it this way.
DefaultPage defaultPage = PageFactory.initElements(this.driver,
DefaultPage.class);
assertTrue(defaultPage.isUserCreateMenuLinkPresent());
Page Object:
Page Object:
public class DefaultPage {
@FindBy(id = "link_i_user_create")
private WebElement userCreateMenuLink;
public boolean isUserCreateMenuLinkPresent() {
try {
this.userCreateMenuLink.getTagName();
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
但我无法相信这个try / catch是应该这样做的方式。 那么什么是更好的方法来检查元素是否退出(使用页面对象)?
But I can not believe that this try/catch is the way one should do it. So what would be a better way to check if the elements exits (with using Page Objects)?
推荐答案
Webdriver被设计为在找不到元素时抛出异常,因此没有任何方法可以验证Webdriver中是否存在元素。
Webdriver is designed to throw an exception if an element is not found, So there aren't any methods to verify presence of an element in Webdriver.
检查 - http://groups.google.com/group/webdriver/browse_thread/thread/909a9b6cb568e341
这篇关于WebDriver:如何检查页面对象Web元素是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文