Selenium:selenium 如何识别元素可见与否?它是否可能在 DOM 中加载但未在 UI 中呈现?

Selenium: How selenium identifies elements visible or not? Is is possible that it is loaded in DOM but not rendered on UI?

  1. Selenium:selenium 如何识别元素可见与否?它是否可能在 DOM 中加载但未在 UI 中呈现? 我想验证元素可点击的场景,我知道网络驱动器有方法 "ElementToBeClickable" 但是,我想知道内部工作原理。请帮我解决这个问题。
  2. 此外,如何处理元素在 DOM 中加载但 UI 显示正在加载的情况,如何等待完成加载?
  3. 请告诉我,如果 UI 没有加载,那么 selenium 会直接调用 DOM 元素,或者如果正在加载 UI 元素,那么它会执行失败吗?我真的很感激对此有更多的技术解释。
  • Selenium可以识别元素的presencevisibility一旦它们 presentvisibleHTML DOM. From user perspective you can invoke isDisplayed() method on an WebElement to examine if the intended WebElement is displayed or not. As per current implementation Selenium may not be distinguishing between loaded and rendered elements. The ElementToBeClickable method in ExpectedConditions class 中设置期望检查元素是否 可见启用以便您可以点击它。

  • 元素加载到 DOM 但 UI 显示正在加载时 你仍然需要 等待 JavaScriptAJAX 调用 完成页面加载,因此所有[页面上的 =38=]WebElements 变为 interactable。最多 等待完成加载 您可以将 pageLoadStrategy 设置为 正常 但可能仍然需要 WebDriverWait预期的 WebElement 成为 presentvisibleinteractable可点击.

在这里你可以找到关于

的详细讨论
  • Of-coarse 如果UI没有加载 Selenium可能无法与一些交互DOM 个元素。

更新

根据你的反问,这里是 WebElement 的不同阶段和相应的 ExpectedConditions 检查阶段:

  • Presence of an element :

     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.
    
  • Visibility of an element :

     visibilityOf(WebElement element)
     An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
    
  • Element to be Clickable :

     elementToBeClickable(By locator)
     An expectation for checking an element is visible and enabled such that you can click it.
    

Note : As per the docs Element is Clickable - it is Displayed and Enabled.