我在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块,但我很好奇为什么这不能按预期工作?
根据下面的代码,有一些基本问题:
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
内部处理NoSuchElementException
和StaleElementReferenceException
而不是WebDriverTimeoutExceptions
。一旦你提到的timespan
初始化WebDriverWait
实例不再存在,在没有看不见的element
你会看到WebDriverTimeoutExceptions
所以从根本上说,将WebDriverTimeoutException
添加到IgnoreExceptionTypes
而ExpectedConditions.InvisibilityOfElementLocated()
为零的结果。