【发布时间】:2019-10-09 09:10:21
【问题描述】:
我必须通过 foreach 循环迭代 List 并且需要单击项目。列表的第一项被点击,但从第二项开始我得到了
org.openqa.selenium.StaleElementReferenceException:过时的元素 参考:元素未附加到页面文档
public void checkFlightAvailabilityToSelectOutBound() throws InterruptedException {
boolean enabledFound=BookDateFlight();
System.out.println("checkFlightAvailabilityToSelectOutBound "+enabledFound);
if(enabledFound==false)
{
List<WebElement> nextAvailableDateList = driver
.findElements(By.xpath("somexpath"));
System.out.println("date list lenth "+nextAvailableDateList.size());
**for (WebElement nxtAvlDate : nextAvailableDateList)
{
try {
System.out.println("------"+nxtAvlDate);
System.out.println("Trying to click on the nxt avl date "+nxtAvlDate.getAttribute("id"));
//wait.until(ExpectedConditions.elementToBeClickable(nxtAvlDate));
//driver.navigate().refresh();
Thread.sleep(3000);
nxtAvlDate.click();
Thread.sleep(5000);**
//wait.until(ExpectedConditions.visibilityOfAllElements(flightInfoOutBoundTravelClassBtnList));
}catch(Exception e) {
e.printStackTrace();
System.out.println("message catch"+e.getMessage());
}
enabledFound=BookDateFlight();
System.out.println("After clicking nxt avil date "+enabledFound);
if(enabledFound==true)
{
break;
}
}
if(enabledFound==false)
{
System.out.println("inside next 7 day");
driver.findElement(By.xpath("//p[@class='next']")).click();
System.out.println("inside next 7 day clicked");
Thread.sleep(10000);
checkFlightAvailabilityToSelectOutBound();
}
}
}
【问题讨论】: