Print All Web Page Links using Selenium WebDriver

We can find every web element using its tag name. Links has anchor tag (a).
In Selenium WebDriver we can use driver.findElements(By.tagName(“a”) to find all links on web page
Run the below Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package scripts;

import org.openqa.package scripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class printAllLinks {

static int linkCount = 0;

public static void main(String[] args) {

//set GeckoDriver path for FireFox browser
System.setProperty("webdriver.gecko.driver", "C:\\harshal personal\\selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");

//Create Browser object
WebDriver driver = new FirefoxDriver();

//Open URL
driver.get("[http://www.seleniumpoint.com](http://www.seleniumpoint.com/)");

//Get links in for each loop

for (WebElement links: driver.findElements(By.tagName("a"))) {
System.out.println(links.getText());
linkCount++;
}

//Print Link Count

System.out.println("Total Links on Page : " + linkCount);

}

}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×