Encountering a StaleElementReferenceException when trying to navigate back in Selenium WebDriver

My current challenge involves crawling through anchors within a web page using Selenium WebDriver. One approach I have considered is gathering the anchors in a list, clicking on each one, and then navigating backwards after each click. Below is the code snippet I am working with:

        WebDriver webDriver=new FirefoxDriver();
        webDriver.get(SEARCH_URL);
        WebElement form2=webDriver.findElement(By.id("frmMain"));
        form2.submit();
        System.out.println(webDriver.getCurrentUrl());
        List<WebElement>doctorAnchors=webDriver.findElements(By.xpath("//td[@class='data']/a"));
        int count=0;
        for(WebElement anchr:doctorAnchors){
            anchr.click();
            System.out.println((count++)+" : "+webDriver.getPageSource().toString());
            Thread.sleep(10000);
            webDriver.navigate().back();
        }

However, when trying to navigate backwards after clicking on an anchor, I encounter the following error:

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 169 milliseconds

After researching similar issues on StackOverflow, I suspect that this error may be caused by JavaScript content on the page. This suspicion aligns with the fact that the page URL remains constant at http://www.somepage.com/dispatch regardless of the anchor clicked. While I can manually navigate back in the web browser opened by the driver, the webDriver.navigate().back() method fails. How can I successfully navigate back after clicking on a link? Is there a way to preserve the state of the driver before clicking and restore it afterwards?

Answer №1

Below is an illustration of a solution using the method getElementWithIndex. It has been tested and proven to work effectively.

In this particular scenario, it retrieves all links within a specified frame, class, or id, and sequentially navigates through each one.

driver.get("www.xyz.com");  
WebElement element = driver.findElement(By.id(Value));
List<WebElement> elements = element.findElements(By.tagName("a"));
int sizeOfAllLinks = elements.size();
System.out.println(sizeOfAllLinks);

for(int i=0; i<sizeOfAllLinks ;i++)
{
     System.out.println(elements.get(i).getAttribute("href"));
}

for (int index=0; index<sizeOfAllLinks; index++ )
{
     getElementWithIndex(By.tagName("a"), index).click();
     driver.navigate().back();
}

public WebElement getElementWithIndex(By by, int index)
{
     WebElement element = driver.findElement(By.id(Value));
     List<WebElement> elements = element.findElements(By.tagName("a")); 
     return elements.get(index);
}

Answer №2

Once you navigate away from the page, the reference to a specific element is lost as it is no longer part of the DOM. To overcome this issue, a simple solution is to retrieve the anchors again upon returning to the page. Essentially, count the number of anchors and use a loop to consistently fetch the anchor list and extract the required anchor from that list.

Answer №3

        WebDriver driver=new FirefoxDriver();
            driver.get(SEARCH_URL);
            WebElement form=driver.findElement(By.id("frmMain"));
            form.submit();
            System.out.println(driver.getCurrentUrl());
            List<WebElement>links=driver.findElements(By.xpath("//td[@class='data']/a"));
            boolean flag = false;
            int count = links.size();
            for(int index=0; index<count; index++){
                if(flag){
                    links=driver.findElements(By.xpath("//td[@class='data']/a"));
                }
                element = links.get(index);

                element.click();
                System.out.println((index++)+" : "+driver.getPageSource().toString());
                Thread.sleep(10000);
                driver.navigate().back();
                flag = true;
                links = new ArrayList<WebElement>();
             }

This is how I approached the task at hand. :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble finding the dynamic input element using Selenium

In my application, there is a textfield that becomes enabled when clicked. I am able to click on it using CSS, but I am having trouble entering text once it is highlighted. I attempted to use xpath to identify the element dynamically, but WebDriver kept t ...

Utilize React to update the state of arrays in functional components

Need help with updating the cars array in my React app. When I click on the Add button, a new object is added to the updatedCars array but the state of cars does not get updated. Even after adding a new object to the array, the initial state remains uncha ...

having difficulty obtaining the 'g-recaptcha-response' for Recaptchav2 using Selenium

I've been facing some challenges while developing a web scraping tool as the data I need is hidden behind a reCaptcha. After researching online, it seems that every captcha contains a TextArea element named 'g-recaptcha-response' which gets ...

How can I prevent javadoc crashes from occurring in Android Studio when using the Maven plugin?

Currently, I am utilizing the com.github.dcendents:android-maven-gradle-plugin to create a POM file and subsequently upload it to Bintray. However, whenever I execute ./gradlew install, the script triggers Maven's install command which FAILS when atte ...

Is it possible to conceal the dates from past months in the datepicker plugin?

Currently, I am utilizing a datepicker tool from jQuery UI and adjusting its CSS to suit my requirements. My goal now is to hide any dates that are not currently active, specifically those displayed from other months. I am unsure if this can be achieved ...

Struggling with getting the .getwidth() and .getheight() functions to function properly

Despite the numerous questions on this topic, I have diligently reviewed all of them but still can't seem to get it to work. Here is the code snippet I have for testing: public class MainActivity extends Activity { RelativeLayout layout; TextView w ...

Filtering in AngularJS can be performed by checking if a value in a specific key of one array is also present as a value in a specific

This query was originally posted on this thread I am looking to implement a filter that will display the values of colors.name only if they also exist in cars.color $scope.colors = [{"name":"blue","count":2}, {"name":"red","count":12}, ...

JavaScript: Choosing between explicit imports and the * sign

Why do this in one way: import * as copy from 'copy-to-clipboard'; instead of the other way: import { someMethod } from 'copy-to-clipboard'; Does it impact performance or bundle size? Personally, I find the second option cleaner. ...

Tips for retrieving javascript-generated HTML content

Currently, I'm attempting to retrieve article headlines from the NY Times website. Upon inspection, it appears that the HTML is being generated by Javascript since it's only visible when using the 'inspect element' feature in Firefox. ...

Avoid unnecessary re-renders in React Redux by ensuring that components do not update when properties are not utilized in their

I'm encountering an issue with a component that keeps re-rendering. I've implemented Redux to handle my states. Within a component, I access a property (isPlaying: bool) from the state using mapStateToProps in various methods of the component, ex ...

Implementing a delay using setTimeOut function to execute an action upon user input

Take a look at this code snippet: $("#test").on("keyup", (e) => { if(e.target.value.length === 3) { setTimeout(() => { console.log("fired") }, 2000) } }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.m ...

There was a problem loading the spec_helper file which caused an error. The issue arose when trying to require "selenium-webdriver" while running seleniumwebdriver in ruby

Encountering an issue while using selenium-webdriver with Ruby, the following error is displayed: λ rspec -fd An error occurred while loading spec_helper. Failure/Error: require "selenium-webdriver" TypeError: unable to resolve type &ap ...

Adding the classname "active" in ReactJS can be achieved by utilizing the `className` attribute within

I am facing an issue with adding the active classname in my code. Can anyone suggest a solution to add the active classname for this section: <li onClick = {() => onChangeStatus({status: 'on-hold'})} className = {appState === {'status& ...

Moving information from Ajax to PHP

I'm experiencing an issue with sending data from AJAX to PHP on the same site, "testpage.php". The PHP script doesn't seem to be displaying the data being sent. Using jQuery/Ajax: <script src="http://code.jquery.com/jquery-latest.js" type="t ...

The value of an AngularJS service is not being reflected in the view

I have implemented the stateProvider in my code, and I am facing an issue with updating the breadcrumbs in the header when the state changes. Instead of creating embedded views for each state, I have a service that shares an array of breadcrumbs containing ...

What is the best method for transferring $scope to a controller using ng-click?

The template I am working with looks like this: <div class="book-thumbs"> <div class="book-pic" ng-repeat="img in book.images"> <img ng-src="{{img}}" ng-click="vm.setImage(img)"> </div> </div> When trying to invo ...

Ember employing the [needs] declaration to establish communication between views

Is it possible to interact between emberViews? I had a setup using controllers that worked well before. >> Index Controller var StudyController = Ember.ArrayController.extend({ needs: ['study/study'], actions: { fi ...

Maximizing your efficiency with Selenium elements: waiting, checking, and clicking without the need to find the elements again

I'm a beginner with Selenium and previously used Telerik's free testing framework. I'm struggling to understand how to interact with elements that have already been identified using [FindsBy]. For example: [FindsBySequence] [Finds ...

Error in Layout of Navigation Panel and Tabbed Pages

Working on a school project, I encountered a challenge. I found two useful solutions from the W3 website - a sticky navigation bar and the ability to include tabs on a single page for a cleaner presentation of information. However, when trying to implement ...

What is the most efficient method in React for displaying an array as a table and wrapping every set of five elements with a <tr> tag?

Currently, I am working on rendering a table in React from an array of objects. My approach involves mapping the array to create table elements as shown below: var objects = response.data; var arrayOfTableElements = [] ...