Selenium assertFalse 因 staleelementreferenceexception 而失败
Selenium assertFalse fails with staleelementreferenceexception
我在 Java 中进行了 selenium 测试,我正在做一些这样的断言:
assertFalse(isElementPresent(By.xpath("//td[2]/div")));
private boolean isElementPresent(By by) {
try { driver.findElement(by); return true; }
catch (NoSuchElementException e) {
return false; }
这是 Selenium 在从 IDE 导出到 Java Webdriver 时生成的标准方法。
(是的,我想断言这个元素不存在)
我在上面的代码行上测试时总是出错
错误:过时的元素引用:元素未附加到 DOM
但是当我在那个步骤前面放一个 thread.sleep 时它起作用了。
我不明白的事实是,等待 1 毫秒就足够了。
在断言之前等待是典型的吗?
还有另一种方法可以解决这个问题吗? (隐式等待在这里没有帮助)
来自德国的问候!
当你在assertFalse()
函数中遇到staleelementreferenceexception时,要否定assertTrue()
函数中的FalsePossitive usecase you can induce WebDriverWait with ExpectedConditions clause set to stalenessOf如下:
Assert.assertTrue(new WebDriverWait(driver, 20).until(ExpectedConditions.stalenessOf(driver.findElement(By.xpath("//td[2]/div")))));
说明
ExpectedConditions 子句 stalenessOf 将检查标识为 (By.xpath("//td[2]/div"))
的元素是否过时。当预期的元素变得陈旧时,您可以检查 assertTrue(boolean condition)
。 assertTrue()
会断言条件为真。如果不是,则会引发 AssertionError。
assertFalse(条件)
如果您仍想实施 assertFalse(condition)
引发 Error 的 FalsePossitive 情况,您仍然可以:
Assert.assertFalse(new WebDriverWait(driver, 20).until(ExpectedConditions.stalenessOf(driver.findElement(By.xpath("//td[2]/div")))));
我认为,超时未设置为 WebDriver。试试这个
assertFalse(isElementPresent(By.xpath("//td[2]/div")));
private boolean isElementPresent(By by) {
driver.timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
try { driver.findElement(by); return true; }
catch (NoSuchElementException e) {
return false; }
我在 Java 中进行了 selenium 测试,我正在做一些这样的断言:
assertFalse(isElementPresent(By.xpath("//td[2]/div")));
private boolean isElementPresent(By by) {
try { driver.findElement(by); return true; }
catch (NoSuchElementException e) {
return false; }
这是 Selenium 在从 IDE 导出到 Java Webdriver 时生成的标准方法。
(是的,我想断言这个元素不存在)
我在上面的代码行上测试时总是出错 错误:过时的元素引用:元素未附加到 DOM
但是当我在那个步骤前面放一个 thread.sleep 时它起作用了。 我不明白的事实是,等待 1 毫秒就足够了。 在断言之前等待是典型的吗? 还有另一种方法可以解决这个问题吗? (隐式等待在这里没有帮助) 来自德国的问候!
当你在assertFalse()
函数中遇到staleelementreferenceexception时,要否定assertTrue()
函数中的FalsePossitive usecase you can induce WebDriverWait with ExpectedConditions clause set to stalenessOf如下:
Assert.assertTrue(new WebDriverWait(driver, 20).until(ExpectedConditions.stalenessOf(driver.findElement(By.xpath("//td[2]/div")))));
说明
ExpectedConditions 子句 stalenessOf 将检查标识为 (By.xpath("//td[2]/div"))
的元素是否过时。当预期的元素变得陈旧时,您可以检查 assertTrue(boolean condition)
。 assertTrue()
会断言条件为真。如果不是,则会引发 AssertionError。
assertFalse(条件)
如果您仍想实施 assertFalse(condition)
引发 Error 的 FalsePossitive 情况,您仍然可以:
Assert.assertFalse(new WebDriverWait(driver, 20).until(ExpectedConditions.stalenessOf(driver.findElement(By.xpath("//td[2]/div")))));
我认为,超时未设置为 WebDriver。试试这个
assertFalse(isElementPresent(By.xpath("//td[2]/div")));
private boolean isElementPresent(By by) {
driver.timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
try { driver.findElement(by); return true; }
catch (NoSuchElementException e) {
return false; }