硒:硒如何识别可见元素?是否可以将其加载到DOM中但不能在UI上呈现? [英] Selenium: How selenium identifies elements visible or not? Is is possible that it is loaded in DOM but not rendered on UI?
问题描述
- 硒:硒如何识别可见元素或不可见元素?是否可以将其加载到DOM中但不能在UI上呈现? 我想验证一个元素可单击的情况,我知道Web驱动器具有方法"ElementToBeClickable",但是,我想知道内部的工作方式.请帮助我.
- 此外,如何处理将元素加载到DOM中但UI显示加载正在进行中的情况,如何等待完整加载?
- 请让我知道,如果未加载UI,则硒将直接调用DOM元素,或者如果加载UI元素,则执行将失败吗?对此,我将不胜感激.
- Selenium: How selenium identifies elements visible or not? Is is possible that it is loaded in DOM but not rendered on UI? I would like to verify a scenario where element is clickable, I know web drive has method "ElementToBeClickable" however, I would like to know the inner working. Please help me on this.
- Also,how to handle a scenario where the element is loaded in the DOM but UI shows loading in progress, how to wait for complete load?
- Please let me know, if the UI is not loaded then will selenium directly call the DOM element or if UI element is being loaded then it will fail the execution? I would really appreciate more technical explanation on this.
推荐答案
-
硒可以在元素存在时立即识别元素的存在或可见性或 HTML DOM 一个>.从用户角度来看,您可以调用 上的
isDisplayed()
方法 WebElement 检查预期的 WebElement 是否显示 .根据当前的实现方式, Selenium 可能无法区分 loaded 和 rendered 元素.ElementToBeClickable 方法/ui/ExpectedConditions.html"rel =" nofollow noreferrer> ExpectedConditions 类为检查元素是否 visible 和 enabled <设置了期望值/em>,以便您可以单击它.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显示加载正在进行中时,您仍然必须等待以获取 JavaScript 和 AJAX调用完成页面的加载,因此页面上的所有 WebElements 都可以可交互.您最多可以将
等待完整的加载,可以将 pageLoadStrategy
设置为 normal ,但可能仍然需要诱使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.
-
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.
这篇关于硒:硒如何识别可见元素?是否可以将其加载到DOM中但不能在UI上呈现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!