Java Selenium 中的 isDisplayed() 与 isVisible()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34380119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:35:57  来源:igfitidea点击:

isDisplayed() vs isVisible() in Selenium

javaseleniumselenium-webdriverwebdriver

提问by MrNolan

What is the difference between isDisplayed()and isVisible()methods in Selenium? Both are used to identify whether web element is or is not hidden in web page.

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

回答by parishodak

As explained in this post How does Selenium WebDriver's isDisplayed() method work

正如这篇文章中所解释的,Selenium WebDriver 的 isDisplayed() 方法是如何工作的

WebDriver has its own W3C specification.and the section about determining visibilitycan give you more information from the spec.

WebDriver 有自己的W3C 规范。而关于确定可见性的部分可以从规范中为您提供更多信息。

Selenium RC - isVisible()- looks for display: none style tag - this might throw a null pointer if we aren't careful...thus to see if an element is visible first check if the element is present using isElementPresent() method. Then try checking if the element is visible! Refer Difference between isElementPresent and isVisible in Selenium RC

Selenium RC - isVisible()- 查找 display: none 样式标签 - 如果我们不小心,这可能会抛出一个空指针......因此要查看元素是否可见首先使用 isElementPresent() 方法检查该元素是否存在. 然后尝试检查元素是否可见!参考Selenium RC 中 isElementPresent 和 isVisible 之间的区别

回答by Erki M.

Short answer is that isVisibleis method of old Selenium RCand isDisplayedis method of Selenium 2.

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

If you are talking about WebDriversWebElement, it contains only isDisplayed()method, which by the doc:

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

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

该元素是否显示?此方法避免了必须解析元素的“样式”属性的问题。

Before webdriver we had Selenium RC, which is now long deprecated, the DefaultSeleniumclass had isVisible()method that:

在 webdriver 之前,我们有 Selenium RC,它现在早已被弃用,DefaultSelenium该类具有以下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.

确定指定的元素是否可见。可以通过将 CSS 的“visibility”属性设置为“hidden”或将“display”属性设置为“none”来使元素不可见,无论是对于元素本身还是对于元素的祖先。如果该元素不存在,则此方法将失败。

reference

参考