Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
1.8k views
by Contributing Tester (92 points)

My goal here is to navigate all hyperlinks on webpage.

Upon trying navigate().back() getting this exception.

Please help.

Code

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class DownloadAllFiles {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "E:\\TestAutomation\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        
        driver.get("https://www.cs.utexas.edu/~scottm/cs307/codingSamples.htm");
        List<WebElement> all_links_webpage = driver.findElements(By.tagName("a")); 
        System.out.println("Total no of links Available: " + all_links_webpage.size());
        int k = all_links_webpage.size();
        System.out.println("List of links Available: ");
        for(int i=0;i<k;i++)
        {
        if(all_links_webpage.get(i).getAttribute("href") != null)
        {

        driver.navigate().to(all_links_webpage.get(i).getAttribute("href"));
        driver.navigate().back();
        }   
        }
        
    }
    
}

Output

Starting ChromeDriver 2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30) on port 46211
Only local connections are allowed.
Jan 15, 2017 5:16:24 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
Jan 15, 2017 5:16:29 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Total no of links Available: 47
List of links Available: 
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 6.1.7601 SP1 x86) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 71 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: 'Ravi-PC', ip: '192.168.145.1', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30), userDataDir=C:\Users\Ravi\AppData\Local\Temp\scoped_dir1580_25788}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=55.0.2883.87, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: b2512a47f89fab0681d3c89f4a7d3987
    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:216)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:274)
    at org.openqa.selenium.remote.RemoteWebElement.getAttribute(RemoteWebElement.java:126)
    at DownloadAllFiles.main(DownloadAllFiles.java:21)

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
selected by
 
Best answer

When you nevigate to the new page, the cache of elements you identified on a previous page will be overridden by new elements. So, you will have to identify those elements again.

        System.out.println("Total no of links Available: " + all_links_webpage.size());
        int k = all_links_webpage.size();
        System.out.println("List of links Available: ");
        for(int i=0;i<k;i++)
        {
        if(all_links_webpage.get(i).getAttribute("href") != null)
        {

        driver.navigate().to(all_links_webpage.get(i).getAttribute("href"));
        driver.navigate().back();

      all_links_webpage = driver.findElements(By.tagName("a")); 
        }   
        }
        
    }
    
}

Hope that helps.

by Contributing Tester (92 points)
Perfect! works.Thanks for help :)


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...