Selenium invisibilityOf(element)方法抛出NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class)不起作用 [英] Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working

查看:331
本文介绍了Selenium invisibilityOf(element)方法抛出NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此查询包含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: Unable to locate 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:NoSuchElementExceptioninvisibilityOfElementLocated()的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:即使我正在使用wait.ignoring(org.openqa.selenium.NoSuchElementException.class);,为什么我仍会得到NoSuchElementException.这是使用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

这里期望的是,该元素必须存在并且可见作为前提条件,并且该方法将等待该元素成为不可见.在这一点上,值得一提的是,由于参数的类型为 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)的前提条件是该元素必须存在于

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.

这篇关于Selenium invisibilityOf(element)方法抛出NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆