硒:硒如何识别可见或不可见的元素?是否有可能在 DOM 中加载但未在 UI 上呈现? [英] Selenium: How selenium identifies elements visible or not? Is is possible that it is loaded in DOM but not rendered on UI?

查看:30
本文介绍了硒:硒如何识别可见或不可见的元素?是否有可能在 DOM 中加载但未在 UI 上呈现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. Selenium:selenium 如何识别可见或不可见的元素?是否有可能在 DOM 中加载但未在 UI 上呈现?我想验证元素可点击的场景,我知道网络驱动器有方法ElementToBeClickable",但是,我想知道内部工作.请帮我解决这个问题.
  2. 另外,如何处理元素已加载到 DOM 中但 UI 显示正在加载的情况,如何等待完全加载?
  3. 请告诉我,如果 UI 没有加载,那么 selenium 会直接调用 DOM 元素,或者如果 UI 元素正在加载,那么它会执行失败吗?我非常希望能就此提供更多技术性解释.

推荐答案

  • Selenium 可以识别元素的 存在可见性,只要它们存在可见HTML DOM.从用户的角度,您可以调用 isDisplayed() 方法在 WebElement 以检查预期的 WebElement 是否显示.根据当前的实现,Selenium 可能无法区分 loadedrendered 元素.ElementToBeClickable 方法在 ExpectedConditions 类设置检查元素是否可见启用 这样你就可以点击它.

    • Selenium can identify the presence or visibility of the elements as soon as they are present or visible in the HTML 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 sets an expectation for checking if an element is visible and enabled so that you can click it.

      元素已加载到 DOM 中但 UI 显示正在加载时,您仍然必须等待 JavaScriptAJAX 调用 以完成页面加载,以便页面上的所有 WebElements 变得可交互.至多等待完全加载,您可以将 pageLoadStrategy 设置为正常,但可能仍然需要引入 WebDriverWait 用于预期WebElement 成为呈现可见可交互可点击.

      When the element is loaded in the DOM but UI shows loading in progress you still have to wait for the JavaScript and AJAX Calls to complete loading the page so all the WebElements on the page becomes interactable. At most to wait for complete load you can set the pageLoadStrategy to normal but may still have to induce WebDriverWait for the intended WebElement to become present, visible, interactable or clickable.

      在这里您可以找到关于页面加载的详细讨论策略

      Here you can find a detailed discussion on Page load strategy

      • 粗略的如果未加载 UI Selenium 可能无法与一些 DOM 元素进行交互.
      • Of-coarse if the UI is not loaded Selenium may not be able to interact with a few of the DOM elements.

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

      As per your counter question here are the different stages of an WebElement and the respective ExpectedConditions to check the stages :

       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.
      

    • 元素的可见性 :

       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.
      

    • 元素可点击 :

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

    • 注意:根据文档元素是可点击的 - 它被显示和启用.

      这篇关于硒:硒如何识别可见或不可见的元素?是否有可能在 DOM 中加载但未在 UI 上呈现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆