In this article, we will talk about finding total number of objects (Links / Checkboxes / dropdown etc) present on the Web Page. We may need these things while counting total number of links present on web page, checking if all links are working properly etc.
Let us see how this can be done.
a. First Navigate to the webpage.
b. Get the list of all web-elements (which you need, could be either links, or checkboxes or dropdowns) using proper attribute type.
c. Get the count (total number of links) from the list.
d. Finally iterate over the list & get all desired objects.
Look at the below example.
Let us suppose, I want to find total number of links present on particular web page.
package basicsOfSelenium;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ExtractAllLInks {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://demowebshop.tricentis.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
List<WebElement> allElements = driver.findElements(By.tagName("a"));
System.out.println("Total Number of Links present on this page is: "+allElements.size());
for (WebElement ele : allElements){
System.out.println("Link is: "+ ele.getText());
}
}
}
Now in case, if you want to find Number of checkboxes present on the web page, then use below –
List<WebElement> listOfCheckBoxes = driver.findElements(By.xpath("//input[@type='checkbox']"));
Now in case, if you want to find Number of dropdowns present on the web page, then use below –
List<WebElement> allDropdowns = driver.findElements(By.tagName("select"));
If you want to find total number of text boxes present on web page, then –
List<WebElement> allTextBoxes = driver.findElements(By.xpath("//input[@type='text'[@class='inputtext']"));
Hope this helps !!!!