2017-06-02 86 views
0

有一个问题,为亚家伙。我正在尝试使用Selenium的流和自动化。java 8和硒

"org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document"

任何想法如何解决这个问题,而不是重写在简单的代码: 我//在这个名单我的forEach得到周围的5个按钮

getAllEditButtons().stream() 
    .limit(2).forEach(webElement -> { 
     webElement.click(); 
     PageUtil.clearInputAndSendKeys(userPassword, password); 
     PageUtil.clearInputAndSendKeys(userReEnterPassword, password); 
     saveClient.click(); 
    }); 

第一次迭代后,我得到了List<WebElement> getAllButtons;

+0

我不明白LAMDA表达式,O/W我可能已经帮你 – kushal

+0

您需要重新鉴定在每次迭代中列出元素,因为某些操作正在使该元素再次加载 – kushal

+0

您可以从流中获取xpath等元素定位器详细信息,并将您的webelement构造为无效陈旧元素。 –

回答

0

看起来您的编辑按钮是在循环中第一次点击后重新创建的。

因此,getAllEditButtons()返回的网页元素将不再存在于第二次迭代中。

为了解决这个问题,你将不得不再次找到网页元素:

IntStream.range(0, getAllEditButtons().size()) 
    .limit(2) 
    .forEach(i -> 
    getAllEditButtons()[i].click(); 
    ... 
);