Selenium assertFalse fails with staleelementreferenceexception

走远了吗. 提交于 2019-11-29 16:31:07

As you are facing staleelementreferenceexception in assertFalse() function, to negate the FalsePossitive usecase you can induce WebDriverWait with ExpectedConditions clause set to stalenessOf within assertTrue() function as follows :

Assert.assertTrue(new WebDriverWait(driver, 20).until(ExpectedConditions.stalenessOf(driver.findElement(By.xpath("//td[2]/div")))));

Explaination

The ExpectedConditions clause stalenessOf will check for the staleness of the element identified as (By.xpath("//td[2]/div")). When the intended element becomes stale, you can check for assertTrue(boolean condition). assertTrue() would assert that a condition is true. If it isn't, an AssertionError would be raised.

assertFalse(condition)

If you still want to implement the FalsePossitive case of assertFalse(condition) raising Error you still can :

Assert.assertFalse(new WebDriverWait(driver, 20).until(ExpectedConditions.stalenessOf(driver.findElement(By.xpath("//td[2]/div")))));

I think, timeouts are not set to WebDriver. try this

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