1

I am trying to read data from a HTML table. The website from where i am trying to read data is gmail Contacts page. Every-time i run the code i am getting -

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 Code which i have written-

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

I am trying to read data inside the contacts page but getting this Exception.

3
  • Can u please point out line no. 36? What is that line doing? Another thing, why r u giving implicitlyWait multiple times. Specifying it once in the driver instance is more than enough. If u r waiting for specific things use explicit wait. And 14000 seconds? That's 3.8 hrs... Commented Jan 21, 2015 at 12:10
  • @VivekSingh The exception is thrown on System.out.print(lst.getText()); inside the for loop. Commented Jan 21, 2015 at 12:51
  • If you are trying to fetch the mail address try to use xpath - //tr[@class='MT']/td[5]/div. Coz am able to fetch the details. Commented Jan 21, 2015 at 13:18

1 Answer 1

2

This is because of the following two reasons from here:

A stale element reference exception is thrown in one of two cases, the first being more common than the second:

The element has been deleted entirely. The element is no longer attached to the DOM.

Generally this happens in ajax based sites. One way is to solve this is to refetch the element, because of ajax call the DOM would have been changed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.