陈旧元素引用异常:如何解决? [英] Stale Element Reference Exception: How to solve?

查看:30
本文介绍了陈旧元素引用异常:如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题陈述:第二次执行 for 循环时出现 Stale Element Exception.

Problem Statement: I am getting Stale Element Exception when for loop executes the second time.

说明:

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

I am using for loop to handle table elements. In the first iteration, it will search for the required element on the page. If the element is not available on that page then it will go and search on the second page. Webdriver successfully finds the element if its available on the first page but if it's not available on the first page then it will look for the element on the second page. But here for loop fails with the exception called 'Stale Element Exception.

错误信息:

线程main"中的异常 org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

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

代码:

 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+",""); 这一行.任何帮助将不胜感激.

I am facing stale element error exception on : String amount = allAmountValues.get(i1+1).getText().replaceAll("\\s+",""); this line. Any help would be appreciated.

推荐答案

原因:

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

allAmountValues is stored initially so when you move between pages, yoou try to use the same elements stored earlier; hgence causing you StaleElementException

解决方案:

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

You have to identify allAmountValuesagain after each time you leave a page and go back to original page.

这篇关于陈旧元素引用异常:如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆