导航到不同的Page并返回后,我得到了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();
}

最佳答案

这是完全正确的行为!一旦导航到任何位置(向前甚至向后),所有WebElement均已失效。您必须更改循环逻辑,如下所示:

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 ...
}

关于java - 导航到其他页面并返回后,我得到了StaleElementReferenceException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46206090/

10-10 14:05