Selenium invisibilityOf(element) method throwing NoSuchElementException + WebDriverWait.ignoring(NoSuchElementException.class) is not working

◇◆丶佛笑我妖孽 提交于 2020-01-30 11:42:44

问题


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));
}

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.

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?

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.

Thanks in advance for your answers.


回答1:


invisibilityOf()

invisibilityOf(WebElement element) is defined as:

public static ExpectedCondition<java.lang.Boolean> invisibilityOf(WebElement element)

An expectation for checking the element to be invisible

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()

invisibilityOfElementLocated(By locator) is defined as:

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.

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.


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.



来源:https://stackoverflow.com/questions/51882118/selenium-invisibilityofelement-method-throwing-nosuchelementexception-webdri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!