2017-04-04 51 views
0

问题声明:当for循环第二次执行时,我得到了陈旧元素异常。陈旧元素引用异常:如何解决?

说明:

我使用for循环来处理表格元素。在第一次迭代中,它将搜索页面上所需的元素。如果该元素在该页面上不可用,那么它将继续并在第二页上进行搜索。 Webdriver成功找到元素,如果元素在第一页上可用,但是如果它在第一页上不可用,那么它会在第二页上查找元素。但是这里的for循环会失败,称为“陈旧元素异常”。

错误消息:在螺纹

异常 “主” 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+","");这一行。 任何帮助,将不胜感激。

回答

1

原因:

allAmountValues存储开始,所以当你在页面之间移动,yoou尝试使用以前存储相同内容; hgence导致你StaleElementException

解决方案:

你必须每次离开一个页面,回到原来的页面时间后再次鉴定allAmountValues

+0

我再次编辑过代码。请检查。 @Kushal,我如何确定allAmountValues的值? – Akki

+0

你必须再次运行这个'List allAmountValues = driver.findElements(By.xpath(“(// table [@ width = 760] [@ cellspacing = 1])[2]/tbody/tr/td [8]/div“));' – kushal

+0

我很害怕,我不明白。导致它在循环中。你能告诉我怎么做吗? – Akki

相关问题