Find all the Links, Checkboxes, textboxes, Menus on a Webpage

Find total number of links on webpage
  • It is 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.
  • In following example we will count and print all the links in URL "https://selenium-vinod.blogspot.in/p/selenium-webdriver.html"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class FindAllLinks {

 public static void main(String[] args) {
  System.setProperty("webdriver.chrome.driver", "C:\\Vinod\\Java\\Workspace\\TestEQ\\drivers\\chromedriver.exe");
  WebDriver driver = new ChromeDriver();
  driver.get("https://selenium-vinod.blogspot.in/p/selenium-webdriver.html");

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

  // Printing size of WenElement links
  System.out.println(links.size());

  for (int i = 1; i <= (links.size() - 1); i = i + 1){
   // Printing text of each link
   System.out.println(links.get(i).getText());
  }
 }
}

Find total number of Checkboxes on a webpage. 
1
2
java.util.List<WebElement> checkboxes = driver.findElements(By.xpath("//input[@type='checkbox']"));
System.out.println(checkboxes.size());

Find total number of Textboxes on a webpage.
1
2
java.util.List<WebElement> textboxes = driver.findElements(By.xpath("//input[@type='text'[@class='inputtext']"));
System.out.println(textboxes.size());

Find total number of Menus on a webpage. 
1
2
java.util.List<WebElement> dropdown = driver.findElements(By.tagName("select"));  
System.out.println(dropdown.size());


<-- Previous || Next -->

No comments:

Post a Comment