org.openqa.selenium.StaleElementReferenceException:陈旧元素引用:元素未附加到页面文档

时间:2015-01-21 11:29:02

标签: java exception selenium arraylist selenium-webdriver

我正在尝试从HTML表中读取数据。我试图读取数据的网站是gmail Contacts页面。 每次我运行代码我都会 -

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=39.0.2171.99)
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86)    (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 602 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html 
 Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
 System info: host: 'Casper-PC', ip: '10.0.0.5', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_25' 
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, chrome={userDataDir=C:\Users\Casper\AppData\Local\Temp\scoped_dir7476_21861},   takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true,  version=39.0.2171.99, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 1e0f05fdc64ecd5720f0d050a602ee4b
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:152)
at com.Trial.SortingTable.main(SortingTable.java:36)

我编写的Java代码 -

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class SortingTable {
    public static void main(String[] agrs) throws InterruptedException{
    WebDriver driver = null;
    try{
    System.setProperty("webdriver.chrome.driver","E:\\chromedriver.exe");
    driver= new ChromeDriver();
    driver.manage().window().maximize();
    //Authentication pop-up
    driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/");
    driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys("Gmail Username");
    driver.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("Gmail Password");
    driver.findElement(By.xpath(".//*[@id='signIn']")).click();
    driver.manage().timeouts().implicitlyWait(14000, TimeUnit.SECONDS);
    WebElement wb = driver.findElement(By.xpath(".//*[@id=':j']"));
    wb.click();
    driver.findElement(By.xpath(".//*[@id=':1h']")).click();
    driver.manage().timeouts().implicitlyWait(14000, TimeUnit.SECONDS);
    Actions action = new Actions(driver);
    action.moveToElement(driver.findElement(By.xpath(".//*[@id=':16r']/div/div[2]"))).perform();
    driver.manage().timeouts().implicitlyWait(14000, TimeUnit.SECONDS);
    driver.findElement(By.xpath(".//a[@title='Other Contacts (16)']")).click();
    driver.manage().timeouts().implicitlyWait(14000, TimeUnit.SECONDS);
    //This is the page which is having the contact 
   List<WebElement> list = driver.findElements(By.xpath(".//*[@id=':17h']/tbody/tr/td[4]/div"));
    for(WebElement lst : list){
       //Trying to read the contact
        System.out.println(lst.getText());
    }//for
    }catch(Exception e){
        e.printStackTrace();
    }//catch
    finally{
        Thread.sleep(4000);
        driver.quit();
        System.exit(0);
    }//finally
  }//main
}//SortingTable

我正在尝试阅读联系人页面中的数据但是获得此异常。

1 个答案:

答案 0 :(得分:2)

这是因为here的以下两个原因:

  

在两种情况之一中抛出过时的元素引用异常,   第一个比第二个更常见:

     

该元素已被完全删除。该元素不再存在   附在DOM上。

通常这种情况发生在基于ajax的网站上。一种方法是解决这个问题是 refetch 元素,因为ajax调用DOM会被更改