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.
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
|
package practiceTestCases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FindAllLinks {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://toolsqa.com/");
java.util.List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println(links.size());
for (int i = 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 to content
HOME
BLOG
FORUM
ABOUT
CONTACTS
Set Up Webdriver & 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:
Find total number of Checkboxes on a Webpage :
|
java.util.List<WebElement> checkboxes = driver.findElements(By.xpath("//input[@type='checkbox']")); System.out.println(checkboxes.size());
|
Find total number of Menus on a Webpage :
|
java.util.List;WebElement> dropdown = driver.findElements(By.tagName("select"));
System.out.println(dropdown.size());
|
Find total number of TextBoxes on a Webpage :
|
java.util.List;WebElement> textboxes = driver.findElements(By.xpath("//input[@type='text'[@class='inputtext']")); System.out.println(textboxes.size());
|
No comments:
Post a Comment