导航到不同的页面并返回后,我得到了StaleElementReferenceException。我尝试了显式等待,再次重新编写定位器,但似乎无效。任何帮助都将受到高度赞赏。
Select selectElement = new Select(dropdown);
List<WebElement> allOptions=selectElement.getOptions();
for (WebElement eachElement : allOptions) {
System.out.println(eachElement.getText());
selectElement.selectByVisibleText(eachElement.getText());
clickElement(selectThisOption);
enterOnlinePage().dataEntry;
changeDistrictPage();
WaitFunctions.waitForPageLoaded(driver);
new Select(dropdown);
//Select selectElement = new Select(dropdown);
allOptions=selectElement.getOptions();
}
答案 0 :(得分:1)
这是完全正确的行为!一旦你在任何地方(向前甚至向后)导航所有,你的Web元素现在都是陈旧的。你必须改变循环的逻辑,如下所示:
Select selectElement = new Select(dropdown);
int count = selectElement.getOptions().size();
for (int i = 0; i < count; i++) {
// must read the element again every time!
Select selectElement = new Select(dropdown);
selectElement.selectByIndex(i);
// the rest of your code ...
}