isDisplayed() 与 Selenium 中的 isVisible()

isDisplayed() vs isVisible() in Selenium

Selenium 中的 isDisplayed()isVisible() 方法有什么区别?两者都用于识别web元素是否隐藏在网页中。

如本文所述post How does Selenium WebDriver's isDisplayed() method work

WebDriver 有自己的 W3C specification. and the section about determining visibility 可以从规范中为您提供更多信息。

Selenium RC - isVisible() - 寻找显示:none 样式标签 - 如果我们不小心,这可能会抛出一个空指针......因此要查看元素是否可见,首先使用 isElementPresent() 方法检查元素是否存在。然后尝试检查元素是否可见!参考 Difference between isElementPresent and isVisible in Selenium RC

简短的回答是 isVisible 是旧 Selenium RC 的方法,isDisplayed 是 Selenium 2 的方法。

如果你在谈论 WebDrivers WebElement,它只包含 isDisplayed() 方法,由文档:

Is this element displayed or not? This method avoids the problem of having to parse an element's "style" attribute.

在 webdriver 之前,我们有 Selenium RC,现在早已弃用,DefaultSelenium class 有 isVisible() 方法:

Determines if the specified element is visible. An element can be rendered invisible by setting the CSS "visibility" property to "hidden", or the "display" property to "none", either for the element itself or one if its ancestors. This method will fail if the element is not present.

reference

根据 v3.141.59 and it's documentation of WebElement Interface 的当前版本,方法 isVisible() 似乎不再受支持并且必须已弃用。

其中 isDisplayed() 得到了很好的支持并且在实践中得到了很好的支持。

boolean isDisplayed()
Is this element displayed or not? This method avoids the problem of having to parse an element's "style" attribute.
Returns:
Whether or not the element is displayed

此函数通常暴露给 GET 具有以下 URI 模板的请求:

/session/{session id}/element/{element id}/displayed