【问题标题】:StaleElementReferenceException: stale element reference: element is not attached to the page documentStaleElementReferenceException:陈旧的元素引用:元素未附加到页面文档
【发布时间】:2019-01-08 02:03:10
【问题描述】:

我在 selenium java 项目中不断收到错误消息。

for(WebElement link:AllTheLinkList) {
    if (link.getAttribute("href") != null
        && ! link.getAttribute("href").contentEquals("javascript"))
    {
        activeLinks.add(link);
    }
}

// Get total amount of links in the page
System.out.println("Size of active links and images --->"
                 + activeLinks.size()); 

错误信息是:

线程“main”中的异常 org.openqa.selenium.StaleElementReferenceException:过时的元素引用:元素未附加到页面文档

【问题讨论】:

    标签: java selenium selenium-webdriver staleelementreferenceexception


    【解决方案1】:

    问题通常是您获取链接的页面在循环期间被刷新。一个解决方案是获取带有 href 值的字符串列表,并且在开始循环后,所有对象都是字符串并且不会改变。

    另一方面,您可以使用数字迭代器进行循环,并且可以再次获得下一次循环迭代之前的最后一条指令AllTheLinkList

    【讨论】:

      猜你喜欢
      • 2021-05-02
      • 1970-01-01
      • 2020-06-29
      • 2021-12-19
      • 2019-09-30
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多