Get all links of website using Selenium webdriver (java)

I do not think it is possible to do by navigating to only one of the page in the web site.

What you can do is for each page you visit you can retrieve the web links separately.

Then combine all these list together into one list and print it as shown below.

// Get all the available Links in Web Page A
    List<WebElement> linksA = driver.findElements(By.tagName("a"));

// Get all the available Links in Web Page B
    List<WebElement> linksB = driver.findElements(By.tagName("a"));

// Combine the two list into one list
    List<String> combineList = Stream.concat(linksA.stream(), linksB.stream()).toList();

// Iterating through all the Links and printing link
    
    for (WebElement link : combineList) {
        System.out.println(link.getText());
        System.out.println(link.getAttribute("href"));
    }

Another solution is when visiting each web page you can use the same variable to add the URLs, so we do not need to combine each list.

Read more here: Source link