【问题标题】:Stale Element Reference Exception: How to solve?过时元素引用异常:如何解决?
【发布时间】:2017-04-04 10:52:44
【问题描述】:

问题说明:当 for 循环第二次执行时,我得到了 Stale Element Exception。

说明:

我正在使用 for 循环来处理表格元素。在第一次迭代中,它将在页面上搜索所需的元素。如果该元素在该页面上不可用,那么它将在第二页上搜索。如果元素在第一页上可用,Webdriver 会成功找到该元素,但如果它在第一页上不可用,则它将在第二页上查找该元素。但是这里的 for 循环失败了,出现了名为“Stale Element Exception”的异常。

错误信息:

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

代码:

 List <WebElement> allAmountValues = driver.findElements(By.xpath("(//table[@width=760][@cellspacing=1])[2]/tbody/tr/td[8]/div"));

     for(int i =0;i<allAmountValues.size();i++){
       System.out.println("Value are : "+allAmountValues.get(i).getText().replaceAll("\\s+", " "));
     }



String testValue = "100000";
 System.out.println("No.of Rows in the Table : "+allAmountValues.size());                

               for (int i1 =1; i1<allAmountValues.size();i1++) {


                    String amount = allAmountValues.get(i1+1).getText().replaceAll("\\s+","");
                    //System.out.println("amount Values : "+amount);
                    System.out.println("Value are : " + allAmountValues.get(i1).getText() + "== Corresponding link is : " + clicklinks.get(i1).getText());

                    if (amount.equalsIgnoreCase(testValue)) {
                        System.out.println("Found:" +testValue);

                        clicklinks.get(i1).click();
                        waitDriver();
                        driver.navigate().back();

                      break;
                    }
                    else
                    {
                        WebElement clickNext = driver.findElement(By.xpath("//a[contains(text(), 'Next')]"));
                        clickNext.click();


                    }


               }


                for(int rw=2;rw<allAmountValues.size();rw++)
                {
                    WebElement a1 = driver.findElement(By.xpath("(//table[@width=760][@cellspacing=1])[2]/tbody/tr["+rw+"]/td[8]/div/span"));
                     String amm = a1.getText().replaceAll("\\s+", "");
                     System.out.println("Current value is:" +amm);
                     if(amm.equalsIgnoreCase("100000"))
                     {
                         WebElement a2 = driver.findElement(By.xpath("/html/body/form/div/table/tbody/tr/td/table/tbody/tr[5]/td/table/tbody/tr["+rw+"]/td[1]/div/input[6]"));
                         a2.click();
                         break;
                     }
                }
                   WebElement authoriseButton = driver.findElement(By.xpath("//input[@name='btnAuthorise']"));
                  if(authoriseButton.isEnabled())
                  {
                      authoriseButton.click();
                  }
                  else
                  {
                      System.out.println("Authorise Button is not enabled");
                  }


         }

我在以下方面面临陈旧元素错误异常: String amount = allAmountValues.get(i1+1).getText().replaceAll("\\s+",""); 这一行。 任何帮助将不胜感激。

【问题讨论】:

    标签: java selenium selenium-webdriver webdriver


    【解决方案1】:

    原因:

    allAmountValues 是最初存储的,因此当您在页面之间移动时,您会尝试使用之前存储的相同元素; hgence 导致你StaleElementException

    解决方案:

    每次离开页面并返回原始页面后,您必须再次识别allAmountValues

    【讨论】:

    • 我再次编辑了代码。请检查。 @Kushal,我如何确定 allAmountValues 的值?
    • 你必须再次运行这个List &lt;WebElement&gt; allAmountValues = driver.findElements(By.xpath("(//table[@width=760][@cellspacing=1])[2]/tbody/tr/td[8]/div"));
    • 恐怕,我不明白。因为它在循环中。你能告诉我怎么做吗?
    • 在 for 循环中添加 List &lt;WebElement&gt; allAmountValues = driver.findElements(By.xpath("(//table[@width=760][@cellspac‌​ing=1])[2]/tbody/tr/‌​td[8]/div")); 作为第一条语句
    • 我在同一位置收到与“陈旧元素异常”相同的错误
    猜你喜欢
    • 1970-01-01
    • 2013-04-16
    • 2018-04-10
    • 2017-10-22
    • 2018-04-01
    • 2017-12-24
    • 1970-01-01
    • 2020-08-04
    • 2021-09-16
    相关资源
    最近更新 更多