Here is how you can find all the Links on a Webpage with Selenium WebDriver in Java.

All you need to do is to create a list of all WebElements, and then iterate thorough the list of links to print the link and the text of the link:

List allLinks = driver.findElements(By.tagName("a"));

System.out.println("All links found on web page are: " + allLinks.size() + " links");

for (WebElement link : allLinks) {

//print the links i.e. http://example.com or https://www.example.com
System.out.println(link.getAttribute("href"));

//print the links text
System.out.println(link.getText());
}

To get the link you can call the “getAttribute” method on the link WebElement passing “href” as its argument which will print the links as i.e. http://example.com or https://www.example.com

LoadFocus.com is a cloud testing platform: