抛出 NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) 的 Selenium invisibilityOf(element) 方法不起作用
Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working
此查询包含 2 个相关问题。
在进入下一步之前,我需要等待一个元素不可见,因此我尝试定义一个自定义方法,如下所示:
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: Unable to locate element:
。
但是,如果我使用定位器方法 new WebDriverWait(world.driver, 5).until(ExpectedConditions.invisibilityOfElementLocated((By.xpath(someXpath))))
,它工作正常,没有任何异常。
问题 1:NoSuchElementException
在 invisibilityOfElementLocated()
的 Selenium 实现中被忽略,但在 invisibilityOf()
中没有。这有什么理由吗?但是,我认为这就是我得到例外的原因。我如何等待元素(不是定位器)消失?
问题 2:为什么我使用 wait.ignoring(org.openqa.selenium.NoSuchElementException.class);
却得到 NoSuchElementException
。 wait.ignoring
的使用方法正确吗?看来 wait.ignoring()
在这里什么也没做。
提前感谢您的回答。
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
不能被 忽略 .
invisibilityOfElementLocated()
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 被调用之前或在 时间跨度期间发生 而 ExpectedCondition 处于活动状态。所以这里我们需要忽略NoSuchElementException
作为强制措施。
回答问题 2:使用 wait.ignoring(org.openqa.selenium.NoSuchElementException.class);
是不合理的,因为调用 invisibilityOf(WebElement element)
的前提条件涉及元素必须是作为强制措施出现在 DOM Tree 中。
此查询包含 2 个相关问题。 在进入下一步之前,我需要等待一个元素不可见,因此我尝试定义一个自定义方法,如下所示:
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: Unable to locate element:
。
但是,如果我使用定位器方法 new WebDriverWait(world.driver, 5).until(ExpectedConditions.invisibilityOfElementLocated((By.xpath(someXpath))))
,它工作正常,没有任何异常。
问题 1:NoSuchElementException
在 invisibilityOfElementLocated()
的 Selenium 实现中被忽略,但在 invisibilityOf()
中没有。这有什么理由吗?但是,我认为这就是我得到例外的原因。我如何等待元素(不是定位器)消失?
问题 2:为什么我使用 wait.ignoring(org.openqa.selenium.NoSuchElementException.class);
却得到 NoSuchElementException
。 wait.ignoring
的使用方法正确吗?看来 wait.ignoring()
在这里什么也没做。
提前感谢您的回答。
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
不能被 忽略 .
invisibilityOfElementLocated()
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 被调用之前或在 时间跨度期间发生 而 ExpectedCondition 处于活动状态。所以这里我们需要忽略NoSuchElementException
作为强制措施。
回答问题 2:使用 wait.ignoring(org.openqa.selenium.NoSuchElementException.class);
是不合理的,因为调用 invisibilityOf(WebElement element)
的前提条件涉及元素必须是作为强制措施出现在 DOM Tree 中。