How to count the no. of options in a drop-down menu using Selenium?
Drop-down menus or drop-down list is one of the most widely used UI (User Interface) elements in web-based applications. It is an indispensable tool for web pages that are used for changing the settings of that web application, forms that require the user to select one option from the list of options (e.g.,) the user’s country of residence. Using selenium we can perform actions on the drop-down list web element, by the methods of the ‘Select’ class.
Counting the no. of options in a drop-down is very frequently needed in writing automation scripts. If we need to know the exact number of options a drop-down should have, we can get the count of those options in that particular drop-down web element. For example, there is a drop-down menu that asks a user to select the gender of the user from the listed options or to select the county of origin. We can find if the count of all the list of genders and the count of all the countries are listed as per the requirement using Selenium.
To count the number of options on any desired web page, we can follow the steps given as follows.
- Inside the main method or ‘@Test’ method (in TestNG), we first declare and initialize an object of the ‘WebDriver’ Interface with ‘ChromeDriver’. This step will open a new Chrome browser tab.
- Using the ‘get(String url)’ method of the ‘WebDriver’ Interface, we can navigate to one of the dedicated students’ practice web pages of Grotechminds “https://grotechminds.com/registration/ “, where students hone their automation skills.
- After navigating to the desired web page, we find the ‘WebElement’ with the ‘select’ tag name using one of the methods of the ‘WebDriver’ Interface called ‘findElement(By by)’ and the ‘By’ method of selenium. Using one of the locators called ‘xpath’, we find the ‘Web Element’ corresponding to the drop-down menu and store it in an object of ‘WebElement’.
- Using the object of the ‘Select’ class, we created by passing the drop-down ‘WebElement’ object as the parameter, we can utilize methods of the ‘Select’ class.
- We store the count of all which the options in the drop-down list in a variable of int data type.
- To get the count of all the options, we use a method called ‘getOptions()’ from the ‘Select’ class has ‘List<WebElement>’ as its return type.
- We then use an abstract class of the ‘List’ Interface called ‘size()’ to count the list of all the options present in the drop-down menu.
- We finally print the result in the console using the ‘println(String x)’ method of the ‘PrintStream’ class.
The drop-down menu we are going to use for counting its options:
public static void main(String[] args) {
WebDriver driver=new ChromeDriver();
driver.get("https://grotechminds.com/registration/");
WebElement dd=driver.findElement(By.xpath("//select[@id='Country']"));
Select sel=new Select(dd);
int count_of_options=sel.getOptions().size();
System.out.println("The total no. of selectable options in the dropdown menu are ="+count_of_options);
}
Output:
The total no. of selectable options in the dropdown menu are =28
Using the steps from the example above, we can count the list of all the options in the drop-down menu ‘WebElement’. This can be used in ‘assertion’ to evaluate the requirement of the drop-down menu ‘WebElement’ with the actual drop-down menu ‘WebElement’ implemented in a webpage. Remember to practise, stay updated with the latest trends in Automation Software Testing Course,and maintain a positive attitude throughout your interview process.
Also read:
Consult Us