【发布时间】: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