硒:硒如何识别可见或不可见的元素?它是否可能在DOM中加载但未在UI上呈现?

时间:2018-02-26 12:55:35

标签: selenium dom selenium-webdriver

  1. 硒:硒如何识别可见或不可见的元素?有可能它是在DOM中加载但不在UI上呈现的吗? 我想验证一个元素是可点击的场景,我知道web驱动有方法" ElementToBeClickable"但是,我想知道内心的工作。请帮帮我。
  2. 此外,如何处理在DOM中加载元素但是UI显示正在加载的方案,如何等待完全加载?
  3. 请告诉我,如果未加载UI,那么selenium会直接调用DOM元素,或者如果正在加载UI元素,那么它将无法执行?我真的很感激对此的更多技术解释。

1 个答案:

答案 0 :(得分:1)

  • Selenium 可以在出现时立即识别元素的状态可见性 HTML DOM中的可见。从用户角度来看,您可以在isDisplayed()上调用WebElement方法,以检查是否 根据当前的实现, Selenium 可能无法区分加载的呈现的元素。 ElementToBeClickable类中的ExpectedConditions方法设置了检查元素是否可见已启用的期望,以便您可以单击 it。

  • 元素加载到DOM但UI显示正在加载时,您仍然必须等待 JavaScript AJAX调用以完成加载页面,以便页面上的所有 WebElements 变为可交互。最多等待完全加载,您可以将pageLoadStrategy设置为正常,但可能仍需要针对目标 WebElement引发WebDriverWait 成为现在可见可互动可点击

    您可以在此处找到有关Page load strategy

  • 的详细讨论
  • 如果未加载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.
    
  

注意:根据文档Element is Clickable - it is Displayed and Enabled