WebDriver:如何检查页面对象Web元素是否存在?

编程入门 行业动态 更新时间:2024-10-25 09:30:10
本文介绍了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.

检查 - groups.google/group/webdriver/browse_thread/thread/909a9b6cb568e341

更多推荐

WebDriver:如何检查页面对象Web元素是否存在?

本文发布于:2023-11-27 16:05:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否存在   元素   对象   页面   WebDriver

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!