It is quite easy to find all the links from a page and it is quite useful in Automation testing. As there will be situations when you want to count all the links from a webpage or to check that none of the links on the webpage are throwing ‘Page not Found‘ errors or there can be a situation when you drive your test through the links present on the web page.

Step to follow…

1) Navigate to the interested webpage for e.g. www.toolsqa.com.

2) Create a list of type WebElement to store all the Link elements in to it.

3) Collect all the links from the webpage. All the links are associated with the Tag ‘a‘.

4) Now iterate through every link and print the Link Text on the console screen.

Java

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

packagepracticeTestCases;

importorg.openqa.selenium.By;

importorg.openqa.selenium.WebDriver;

importorg.openqa.selenium.WebElement;

importorg.openqa.selenium.firefox.FirefoxDriver;

publicclassFindAllLinks{

publicstaticvoidmain(String[]args){

WebDriver driver=newFirefoxDriver();

driver.get("http://toolsqa.com/");

java.util.List<WebElement>links=driver.findElements(By.tagName("a"));

System.out.println(links.size());

for(inti=1;i<=links.size();i=i+1)

{

System.out.println(links.get(i).getText());

}

}

}

Output will be like this:

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

90

Skip tocontent

HOME

BLOG

FORUM

ABOUT

CONTACTS

Set Up Webdriver&amp;Eclipse

Selenium Webdriver Basics

Selenium with Excel POI

Selenium with Log4j

Selenium with TestNG

Selenium Automation Framework

Selenium Grid

Selenium Blogs

Selenium Best Practices

Please leave your Feedback

The same way you can easily be able to find any type of WebElements on a WebPage: