0

I am trying to iterate through the links in a section of the footer. I have written this code but it keeps failing. I hope someone will be able to tell me what I am doing wrong and how can I fix this.

import java.awt.List;
import java.util.Iterator;
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;

public class HomeWork {

public static void main(String[] args) throws InterruptedException {
    System.setProperty("webdriver.chrome.driver", "C:\\Work\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    String url = "http://automationpractice.com/index.php?controller=authentication&back=my-account";
    driver.get(url);



    WebElement infoLinkSec = driver.findElement(By.xpath("//*[@id=\"block_various_links_footer\"]"));
    int noOfLink = infoLinkSec.findElements(By.tagName("a")).size();
    System.out.println(noOfLink);


    Iterator<WebElement> links = infoLinkSec.findElements(By.tagName("a")).iterator();





    WebElement[] link = new WebElement[noOfLink];


    for (int i = 0; i< noOfLink; i++ ) {
        link[i] = links.next();
            }


    for (int i = 0; i< noOfLink; i++ ) {

        link[i].click();
        Thread.sleep(2000L);

        String cUrl = driver.getCurrentUrl();
        String cPageTitle = driver.getTitle();
        System.out.println((i+1) +". The current url is "+cUrl+"\nThe current page title is "+cPageTitle);

        driver.navigate().back();
        driver.navigate().refresh();
        Thread.sleep(3000L);
        }


}

    }

When I try to open anylink using link[3].click(); or link[4].click(); it will open the link, The problem seems to be when we go back and refresh, that is when it would open another link.

The ERROR is :

Starting ChromeDriver 78.0.3904.70         (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-    heads/3904@{#800}) on port 5525
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test     frameworks to prevent access by malicious code.
[1575300793.350][WARNING]: Timed out connecting to Chrome,     retrying...
Dec 02, 2019 10:33:15 AM     org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
[1575300797.392][WARNING]: Timed out connecting to Chrome,     retrying...
8
1. The current url is     http://automationpractice.com/index.php?controller=prices-drop
The current page title is Prices drop - My Store
Exception in thread "main"     org.openqa.selenium.StaleElementReferenceException: stale     element reference: element is not attached to the page     document
  (Session info: chrome=78.0.3904.108)
For documentation on this error, please visit:     https://www.seleniumhq.org/exceptions/stale_element_reference.    html
Build info: version: '3.141.59', revision: 'e82be7d358',     time: '2018-11-14T08:25:48'
System info: host: 'LAPTOP-P9CFNEOV', ip: '192.168.56.1',     os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0',     java.version: '1.8.0_222'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName:     chrome, browserVersion: 78.0.3904.108, chrome:     {chromedriverVersion: 78.0.3904.70 (edb9c9f3de024...,     userDataDir: C:\Users\YSAMSE~1\AppData\L...},     goog:chromeOptions: {debuggerAddress: localhost:54615},     javascriptEnabled: true, networkConnectionEnabled: false,     pageLoadStrategy: normal, platform: XP, platformName: XP,     proxy: Proxy(), setWindowRect: true,     strictFileInteractability: false, timeouts: {implicit: 0,     pageLoad: 300000, script: 30000}, unhandledPromptBehavior:     dismiss and notify}
Session ID: 2377347e6ccb38499c3f86e0b8cc0862
    at     sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native     Method)
    at     sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeCo    nstructorAccessorImpl.java:62)
    at     sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Dele    gatingConstructorAccessorImpl.java:45)
    at     java.lang.reflect.Constructor.newInstance(Constructor.java:423    )
    at     org.openqa.selenium.remote.http.W3CHttpResponseCodec.createExc    eption(W3CHttpResponseCodec.java:187)
    at     org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3    CHttpResponseCodec.java:122)
at     org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3    CHttpResponseCodec.java:49)
    at     org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCom    mandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execu    te(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
at HomeWork.main(HomeWork.java:50)
YSam Kabir
  • 51
  • 1
  • 1
  • 6

1 Answers1

0

So, it looks like you are getting a StaleElementReferenceException here. You said "The problem seems to be when we go back and refresh", which is exactly correct -- that's how you encounter this exception.

When you find a WebElement or list of WebElement and store it in a variable, then change the page (through refreshing or going to a different page), your reference to this element is no longer valid. Your variable is pointing to an element that has gone stale.

This issue can be fixed by re-finding all of the elements you are interacting with after you refresh the page.

So, after driver.navigate.refresh();, link[i].click() will throw the exception, because link[] now contains a list of elements that are no longer valid on the page. You will need to re-call driver.findElements to set link[] back to a state of fresh elements.

Your code to set the link[] variable seems a bit complex here, so it's not as easy as just calling driver.findElements after you refresh. You may need to refactor the code so that you can easily find a fresh set of links after each refresh:

System.setProperty("webdriver.chrome.driver", "C:\\Work\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

String url = "http://automationpractice.com/index.php?controller=authentication&amp;back=my-account";
driver.get(url);

// get the links
List<WebElement> links = driver.findElement(By.xpath("//*[@id='block_various_links_footer']//a"));

// iterate links in a for loop
for (int i = 0; i < links.size(); i++)
{
    // click the link
    link[i].click();
    Thread.sleep(2000L);

    // print page info
    String cUrl = driver.getCurrentUrl();
    String cPageTitle = driver.getTitle();
    System.out.println((i+1) +". The current url is "+cUrl+"\nThe current page title is "+cPageTitle);

    // go back
    driver.navigate().back();
    driver.navigate().refresh();
    Thread.sleep(3000L);

    // re-find links list
    List<WebElement> links = driver.findElement(By.xpath("//*[@id='block_various_links_footer']//a"));
}

Selenium gave you a helpful link to their documentation on StaleElementReferenceException in your stack trace. You can read more on their page if you're extra curious.

CEH
  • 5,701
  • 2
  • 16
  • 40
  • Hi Christine; Thank you so much for your help, I really appreciate the time and effort that you put in helping me. Christine, I am rewritten the code as you suggested but I am still getting the same error. Do you think the iterator() might not the best way to click on the links, and if you were to do this what would your approach look like. Thank you and I really appreciate your help. – YSam Kabir Dec 03 '19 at 07:23
  • @YSamKabir I've updated the answer with a simplified version of your code. This was tested in C# but converted to Java, the functionality should be the same. – CEH Dec 03 '19 at 15:32