为什么有些元素存在但不可交互/显示? [英] Why do some elements exist but not interactable/displayed?
问题描述
我对测试还很陌生,试图更好地了解到底发生了什么.我发现当 css 选择器元素附加有 waitUntilCanInteract 或 waitUntilDisplayed 时,我们的一些测试代码失败了,即使当我进行 chrome 检查时该元素显示在浏览器中.将它们更改为 waitUntilExists 会使它们到达一个通过点,所以我想知道究竟是什么导致了这种情况?
I'm pretty new to testing, trying to gain a better understanding of what exactly is going on. I'm finding some of our test codes are failing when the css selector element has a waitUntilCanInteract or waitUntilDisplayed attached to it even though when I do a chrome inspect the element is showing up in the browser. Changing them to a waitUntilExists gets them to a passing point so I was wondering what exactly is going on to create this situation?
推荐答案
精确Selenium 处理元素的三种独特状态.
Precisesly Selenium deals with three unique states of an element.
元素的存在 html:可以通过 ExpectedCondition 检测元素的这种状态
presenceOfElementLocated()
其中期望检查元素是否present 在页面的 DOM 中.这并不一定意味着该元素可见.
Presence of element within the html: This state of an element can be detected through the ExpectedCondition
presenceOfElementLocated()
where the expectation is to check if the element is present in the DOM of a page. This does not necessarily mean that the element is visible.
示例:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("css_of_element")));
元素在 html>:可以通过 ExpectedCondition 检测元素的这种状态 visibilityOfElementLocated()
其中期望检查元素是否present 在页面的 DOM 中并且可见.可见性意味着元素不仅被显示,而且高度和宽度大于0.
Visibility of element within the html: This state of an element can be detected through the ExpectedCondition visibilityOfElementLocated()
where the expectation is to check if the element is present in the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
示例:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("css_of_element")));
元素可点击:可以通过ExpectedCondition检测元素的这种状态elementToBeClickable()
期望检查元素是否可见和启用,以便您可以点击它.
Element to be clickable: This state of an element can be detected through the ExpectedCondition elementToBeClickable()
where the expectation is to check if the element visible and enabled so that you can click it.
示例:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("css_of_element")));
这篇关于为什么有些元素存在但不可交互/显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!