---
title: "selenium-webdriver: element is not attached to the page问题解决"
author: "jerome_wang"
date: "2014-07-17T14:03:56Z"
tags: []
categories: ["行业应用"]
slug: "selenium-webdriver-element-is-not-attached-to-the-page"
draft: false
lastmod: "2014-07-17T14:03:56Z"
id: 2093139
url: "https://jerome-wang.iteye.com/blog/2093139"
---

现象： 今天在遍历选择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&lt;WebElement&gt; options = clsSelect.getOptions();     
            for(int e = r; e &lt; options.size(); e++){  
                if(options.get(e).isSelected()){  
                    continue;  
                }else{  
                    clsSelect.selectByIndex(e);  
                    r = e;  
                }  
            }  
        }catch(StaleElementReferenceException e){  
            selectOption(driver,by);  
        }  
     
    }