【问题标题】:Not able to resolve stale element reference: element is not attached to the page document无法解析过时的元素引用:元素未附加到页面文档
【发布时间】:2021-06-25 03:37:33
【问题描述】:

我遇到过时的元素异常并尝试添加 try catch 块,但我仍然收到此错误。有没有办法解决它?我想单击每个 div 中包含文本“Dep.Libraries”的特定链接。

        List<WebElement> lib = driver.findElements(By.className("row"));
        ListIterator<WebElement> listlib = lib.listIterator();
        System.out.println(listlib);
        while(listlib.hasNext()) {
             WebElement elem = listlib.next();
             List<WebElement> sub = elem.findElements(By.cssSelector("div[class='column sp']"));
             ListIterator<WebElement> listsub = sub.listIterator();
             while(listsub.hasNext()) {
                WebElement elem1 = listsub.next();          
                if((elem1.getText()).contains("Dep. Libraries")) {          
                elem1.click();
                break;
                }
             }
        }
    

【问题讨论】:

  • 什么是错误请编辑您的问题

标签: selenium dom staleelementreferenceexception


【解决方案1】:

有两种方法可以解决陈旧元素错误

  1. 在交互元素之前刷新页面(使用 Refresh();)
  2. 尝试循环直到捕获特定元素

【讨论】:

  • 谢谢,我尝试了两种方法,但仍然没有运气。我想单击存储在 listsub 中的每个元素。
猜你喜欢
  • 2021-12-15
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多