1

My coding logic:

List<WebElement> getelement = driver
                .findElements(By.xpath("//*[@class='footer__content']//a[@class='footer-nav__link']"));

        WebDriverWait wait=new WebDriverWait(driver, 60000);
for (int i = 0; i < getelement.size(); i++) {                       
            System.out.println("clicking on this link::" + getelement.get(i).isDisplayed());
            getelement.get(i).click();
            Thread.sleep(15000);
            driver.navigate().back();
            Thread.sleep(30000);

        }

Error is :

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=74.0.3729.108)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 6.3.9600 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z'
System info: host: 'SHASHANK', ip: '169.254.91.79', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.35.528161 (5b82f2d2aae0ca..., userDataDir: C:\Users\admin\AppData\Loca...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 74.0.3729.108, webStorageEnabled: true}
Session ID: 844f1399541d4bb05f1922d9194602a0
    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:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)
    at org.openqa.selenium.remote.RemoteWebElement.isDisplayed(RemoteWebElement.java:320)
    at Login.Footer_Link.main(Footer_Link.java:35)
7
  • It is throwing StaleElementReferenceException means on which element you wan to perform an action that element is not attached on DOM. Apr 30, 2019 at 9:21
  • Also your WebDriverWait object has been created but after the element, so your Explicit wait is also not working on this code. Apr 30, 2019 at 9:24
  • Yes, I know It but when I move to back that time next link element is not getting
    – Jay Pathak
    Apr 30, 2019 at 9:49
  • after click on link, does you page open on new window? Apr 30, 2019 at 9:50
  • No, It's open in same page and also move to back by using browser back By using :driver.navigate().back();
    – Jay Pathak
    Apr 30, 2019 at 9:53

1 Answer 1

1

This does not work, you cannot click a link that loads another page. Then go back and expect the earlier found elements to still be there in memory.

        getelement.get(i).click();
        driver.navigate().back();

You need to again find the elements after back. Changed your example code a bit to retrieve the elements again in the loop.

By path = By.xpath("//a");
List<WebElement> elementsCount = driver.findElements(path).size();

for (int i = 0; i < elementsCount ; i++) {     
            List<WebElement> getelement = driver.findElements(path);                  
            System.out.println("clicking on this link::" + getelement.get(i).isDisplayed());
            getelement.get(i).click();
            Thread.sleep(15000);
            driver.navigate().back();
            Thread.sleep(30000);
}
1
  • 1
    Thanks It's resolve now.
    – Jay Pathak
    Apr 14, 2023 at 14:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.