VB.Net Selenium WebDriverWait.IgnoreExceptionTypes不抑制WebDriverTimeoutException

问题描述 投票:-1回答:1

我在Selenium中使用ExplicitWait时试图忽略WebDriverTimeoutException,就像这样。

Dim wait As New WebDriverWait(driver, New TimeSpan(0, 0, 10))
wait.IgnoreExceptionTypes(GetType(WebDriverTimeoutException))
wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.Id("foo")))

这适用于其他Selenium异常,例如NoSuchElementException。但是使用WebDriverTimeoutExceptions时,它不会被忽略。

我意识到我可以使用Try Catch块,但我很好奇为什么这不能按预期工作?

.net vb.net selenium
1个回答
0
投票

根据下面的代码,有一些基本问题:

Dim wait As New WebDriverWait(driver, New TimeSpan(0, 0, 10))
wait.IgnoreExceptionTypes(GetType(WebDriverTimeoutException))
wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.Id("foo")))

当你用WebDriverWait子句设置为ExpectedConditions诱导InvisibilityOfElementLocated时,这有效地意味着element元素必须存在于HTML DOM中,你想要等待它的隐形。如果你看一下Python的源代码(我看了InvisibilityOfElementLocated),它被定义为:

try:
    return _element_if_visible(_find_element(driver, self.locator), False)
except (NoSuchElementException, StaleElementReferenceException):
    # In the case of NoSuchElement, returns true because the element is
    # not present in DOM. The try block checks if the element is present
    # but is invisible.
    # In the case of StaleElementReference, returns true because stale
    # element reference implies that element is no longer visible.
    return True

这意味着InvisibilityOfElementLocated内部处理NoSuchElementExceptionStaleElementReferenceException而不是WebDriverTimeoutExceptions。一旦你提到的timespan初始化WebDriverWait实例不再存在,在没有看不见的element你会看到WebDriverTimeoutExceptions

所以从根本上说,将WebDriverTimeoutException添加到IgnoreExceptionTypesExpectedConditions.InvisibilityOfElementLocated()为零的结果。

© www.soinside.com 2019 - 2024. All rights reserved.