user4460330
user4460330

Reputation:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

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.

Upvotes: 1

Views: 4514

Answers (1)

Ant&#39;s
Ant&#39;s

Reputation: 13811

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.

Upvotes: 2

Related Questions