微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ExpectedConditions.visibilityOfElementLocated() 和 ExpectedConditions.presenceOfElementLocated() 之间的区别

如何解决ExpectedConditions.visibilityOfElementLocated() 和 ExpectedConditions.presenceOfElementLocated() 之间的区别

我有两个问题:

1)ExpectedConditions.visibilityOfElementLocated()ExpectedConditions.presenceOfElementLocated() 有什么区别?我是在可见和存在的上下文中问的?这里的 visiblepresence 是什么意思?可见是否意味着在最大化的浏览器窗口中可见?

2)如果我使用 ExpectedConditions.visibilityOfElementLocated() 并最小化浏览器会怎样?它会给出 TimeoutException 吗?

解决方法

https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#visibilityOf(org.openqa.selenium.WebElement)

visibilityOfElementLocated:

公共静态预期条件 visibilityOfElementLocated (By locator) 期望检查 元素存在于页面的 DOM 上并且可见。 可见性意味着元素不仅可以显示,而且还具有 大于 0 的高度和宽度。 参数: locator - 用于 找到该元素返回:WebElement 一旦它被定位和 可见

presenceOfElementLocated

公共静态预期条件 PresenceOfElementLocated (By locator) 期望检查 一个元素存在于页面的 DOM 上。这不一定 表示元素可见。参数: locator - 用于查找 元素返回:定位后的 WebElement

在可见性中,它检查显示属性以及图像的渲染矩形大小

目前它只是检查元素是否存在于 DOM 中,

仅当我们将鼠标悬停在其上时才显示的工具提示文本示例,该元素是否始终存在但仅在我们将鼠标悬停在其上时才可见

,

presenceOfElementLocated()

presenceOfElementLocated() 是检查元素是否存在于页面的 DOM 上的期望值。它定义为:

public static ExpectedCondition<WebElement> presenceOfElementLocated​(By locator)
An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.

Parameters:
locator - used to find the element

Returns:
the WebElement once it is located

所以presenceOfElementLocated 不能确认元素在DOM Tree可见。这种现象可以通过 WebElement 观察到:

  • CSS visibility 属性设置为 hidden。举个例子:

    h2.b {
      visibility: hidden;
    }   
    
  • style 属性设置为 "display: none;"。一个例子:

    <select class="pretty-dropdown" id="alBrandsList" style="display: none;">
    

visibilityOfElementLocated()

visibilityOfElementLocated() 是检查元素是否存在于页面的 DOM 上并且可见的期望。定义为:

public static ExpectedCondition<WebElement> visibilityOfElementLocated​(By locator)
An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

Parameters:
locator - used to find the element

Returns:
the WebElement once it is located and visible

visibilityOfElementLocated() 最小化浏览器

最小化浏览器的可见性/不可见性受我们肉眼的影响,而presenceOfElementLocated /visibilityOfElementLocated 是Selenium 的主观感受。

您可以在How to execute tests with selenium webdriver while browser is minimized

中找到相关的详细讨论

参考文献

您可以在以下位置找到一些关于元素 displayedness 的相关详细讨论:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。