答案 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 来检查阶段:
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.