gpt4 book ai didi

java - 过时元素引用异常 : How to solve?

转载 作者:行者123 更新时间:2023-12-02 13:30:37 26 4
gpt4 key购买 nike

问题陈述:当 for 循环第二次执行时,我收到 Stale Element Exception。

描述:

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

错误消息:

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

最佳答案

原因:

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

解决方案:

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

关于java - 过时元素引用异常 : How to solve?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43205160/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com