【问题标题】:Difference between ExpectedConditions.visibilityOfElementLocated() and ExpectedConditions.presenceOfElementLocated()ExpectedConditions.visibilityOfElementLocated() 和 ExpectedConditions.presenceOfElementLocated() 之间的区别
【发布时间】:2021-05-19 06:45:31
【问题描述】:

我有两个问题:

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

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

【问题讨论】:

    标签: selenium selenium-webdriver selenium-chromedriver ui-automation


    【解决方案1】:

    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() 浏览器最小化

    最小化浏览器的可见性/不可见性取决于我们的肉眼,因为存在于元素位置 / 可见性元素位置取决于 Selenium

    您可以在How to execute tests with selenium webdriver while browser is minimized找到相关的详细讨论


    参考文献

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

    【讨论】:

      【解决方案2】:

      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

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

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

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

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

      【讨论】:

        猜你喜欢
        • 2016-01-19
        • 2016-01-19
        • 1970-01-01
        • 2021-12-25
        • 2020-05-10
        • 2014-09-20
        • 2010-10-28
        • 2015-10-04
        • 2012-08-12
        相关资源
        最近更新 更多