问题描述
此查询包含 2 个相关问题.在进行下一步之前,我需要等待一个元素不可见,因此我尝试定义一个自定义方法,如下所示:
This query consists of 2 related questions. I need to wait for an element to be invisible before I go to next step, hence I tried to define a custom method as below:
public void waitToDisappear(long timeOutInSeconds, WebElement element) {
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.ignoring(org.openqa.selenium.NoSuchElementException.class);
wait.until(ExpectedConditions.invisibilityOf(element));
}
当我将此方法称为 common.waitToDisappear(5, <WebElement>);
时,我得到 Caused by: org.openqa.selenium.NoSuchElementException: no such element: 无法定位元素:
.但是,如果我使用定位器方法 new WebDriverWait(world.driver, 5).until(ExpectedConditions.invisibilityOfElementLocated((By.xpath(someXpath))))
,它可以正常工作,没有任何异常.
When I call this method as common.waitToDisappear(5, <WebElement>);
, I'm getting Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:
.
However, if I use locator method new WebDriverWait(world.driver, 5).until(ExpectedConditions.invisibilityOfElementLocated((By.xpath(someXpath))))
, it is working fine without any exception.
问题 1:NoSuchElementException
在 invisibilityOfElementLocated()
的 Selenium 实现中被忽略,但在 invisibilityOf()
中没有.这有什么原因吗?但是,我认为这就是我得到例外的原因.如何等待元素(不是定位器)消失?
Question-1: NoSuchElementException
is ignored in Selenium implementation of invisibilityOfElementLocated()
, but not in invisibilityOf()
. Is there is any reason for this? But, I think this is why I'm getting exception. How do I wait for an element(not locator) to be disappeared?
问题 2:为什么我得到 NoSuchElementException
,即使我使用的是 wait.ignoring(org.openqa.selenium.NoSuchElementException.class);
.这是使用 wait.ignoring
的正确方法吗?似乎 wait.ignoring()
在这里没有做任何事情.
Question-2: Why am I getting NoSuchElementException
even though I'm using wait.ignoring(org.openqa.selenium.NoSuchElementException.class);
. Is it the right way of using wait.ignoring
? It seems that wait.ignoring()
is not doing anything here.
提前感谢您的回答.
推荐答案
invisibilityOf()
invisibilityOf(WebElement element)
定义为:
public static ExpectedCondition<java.lang.Boolean> invisibilityOf(WebElement element)
An expectation for checking the element to be invisible
这里的期望是,元素必须是 present 以及 visible 作为前置条件,并且该方法将等待元素是 不可见.此时值得一提的是,由于参数是WebElement 类型,findElement(By by)
必须成功定位元素作为前提条件.因此 NoSuchElementException
不能被忽略.
Here the expectation is that, the element must be present as well as visible as a pre-condition and the method will wait for the element to be invisible. At this point it is worth to mention that as the argument is of type WebElement, findElement(By by)
have to successfully locate the element as a pre-condition. Hence NoSuchElementException
can't be ignored.
invisibilityOfElementLocated(By locator)
定义为:
public static ExpectedCondition<java.lang.Boolean> invisibilityOfElementLocated(By locator)
An expectation for checking that an element is either invisible or not present on the DOM.
这里的期望显然是元素已经 不可见 或 不存在 在 HTML DOM.在这种情况下,主要任务是元素的缺席,它甚至可能在 ExpectedCondition 被调用之前或在 timespan 期间发生,而 ExpectedCondition 处于活动状态.所以这里我们需要忽略 NoSuchElementException
作为强制措施.
Here the expectation is clearly either the element is already invisible or not present in the HTML DOM. In this case the primary mission is the absence of the element which may occur even before the ExpectedCondition is invoked or during the timespan while the ExpectedCondition is active. So here we need to ignore NoSuchElementException
as a mandatory measure.
回答 问题 2:使用 wait.ignoring(org.openqa.selenium.NoSuchElementException.class);
不能作为调用 invisibilityOf(WebElement element)
涉及元素必须存在于 DOM 树 作为强制性措施.
Answering Question-2: Using wait.ignoring(org.openqa.selenium.NoSuchElementException.class);
isn't justified as the pre-condition of invoking invisibilityOf(WebElement element)
involves the fact that the element must be present in the DOM Tree as a mandatory measure.
这篇关于抛出 NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) 的 Selenium invisibilityOf(element) 方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!