1

My goal is:

  • Iterate on WebElements in a webpage
  • Click on all elements founded and open link in same session
  • Parse the new page with some other logic
  • Return back to prev page and continue the loop for all prev matched id

I have this code:

List<WebElement> links = driver.findElements(By.cssSelector("div[data-sigil='touchable']"));

// this will display list of all images exist on page
for(WebElement ele:links)
{
    System.out.println("test->"+ele.getAttribute("id"));
    ele.click();
    Thread.sleep(500);
    System.out.println("URI->"+driver.getCurrentUrl());
    js.executeScript("window.history.go(-1)");
} 

return "ok";

Which is working fine and it finds correct elements id, "ele.click()" is actually working, but I'm always failing when I execute js.executeScript("window.history.go(-1)")

This is my error message:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=73.0.3683.103)
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

So basically I'm not able to continue the loop. Is it there any useful technique to "click into new tab" and manage different Selenium driver session?

Thanks a lot in advance for any suggestion.

3
  • 1
    I believe your js variable is an WebElement and has changed because the page has changed, you need to re-lookup the element. Commented Apr 18, 2019 at 8:54
  • thx, but how do I mark webelements ID that I've already processed? Commented Apr 18, 2019 at 9:29
  • Thats a different question. You would need to find something unique about each link (like the url), add it to a dictionary or list and then lookup the whole list and remove ones that are in the dictionary / list. Commented Apr 18, 2019 at 9:32

2 Answers 2

1

What is happening is that when you proceed to another page it makes all the elements in the list stale. Those elements are not attached to the page when you come back to the page again. You need to find the elements every time you load the page.

Try this:

List<WebElement> links = driver.findElements(By.cssSelector("div[data-sigil='touchable']"));
        // this will display list of all images exist on page
String address;
        for(int i=0; i<links.size(); i++){
            address = driver.getCurrentUrl();
            links = driver.findElements(By.cssSelector("div[data-sigil='touchable']"));
            System.out.println("size: "+links.size());
            WebElement ele = links.get(i);
            System.out.println("test->"+ele.getAttribute("id"));
            ele.click();
            Thread.sleep(500);
            System.out.println("URI->"+driver.getCurrentUrl());
            //js.executeScript("window.history.go(-1)");
            //driver.navigate().back();
            driver.get(address);
        }

Edit:

Try the driver.get() as it waits for the page to load. Or you can directly add another sleep as you used after the click.

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

7 Comments

Thanks, I got this error: ava.lang.IndexOutOfBoundsException: Index: 2, Size: 0 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at
hello, even with driver.get() is returning same error. where should I check for explicit wait exactly? sorry i'm bit puzzled on this - Thanks a lot
Since it returned size 0, I'm assuming it didn't wait for the page to load. I've edited the answer. Could you try this again? Also, is there any chance to change the number of elements on reload? If it changes then the solution will not work.
Hi, same error, but I was thinking: is there a way to open a new tab? or new session in selenium? like I got all the elements id, click it and open in a new session , do the stuff and kill it. thx
Hi, I've edited with the driver.get() code. could you please use this and let me know if it is working or not? Also please check if the size of the list remains the same.
|
1

I think you need to create the js object, like so.

Reason being that you "lost" the reference to the JavascriptExecutor

List<WebElement> links = driver.findElements(By.cssSelector("div[data-sigil='touchable']"));
// this will display list of all images exist on page
for(WebElement ele:links){
    System.out.println("test->"+ele.getAttribute("id"));
    ele.click();
    Thread.sleep(500);
    System.out.println("URI->"+driver.getCurrentUrl());
    // Re initialise js executor
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("window.history.go(-1)");
} 
return "ok";

1 Comment

I dont think the error is occurring when you run the following line (as per OP) js.executeScript("window.history.go(-1)"); I suspect its when the for loop tries to iterate to the next item in links.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.