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?