selenium-webdriver: element is not attached to the page问题解决

现象: 今天在遍历选择select option 时出现:element is not attached to the page document

原因:The reason was because javascript loaded the element one more time after i have referred so my date reference pointed to an unexisting object even if it was right their on UI

解决方案:select 为例

    private void selectOption(WebDriver driver,By by) {
        try{
           
            Select clsSelect = new Select(driver.findElement(by));
            List<WebElement> options = clsSelect.getOptions();   
            for(int e = r; e < options.size(); e++){
                if(options.get(e).isSelected()){
                    continue;
                }else{
                    clsSelect.selectByIndex(e);
                    r = e;
                }
            }
        }catch(StaleElementReferenceException e){
            selectOption(driver,by);
        }
   
    }

你可能感兴趣的:(webdriver)