Fri. Apr 19th, 2024
Introduction to dropdown in seleniumIntroduction to dropdown in selenium

A webpage will contain various elements like text fields, checkboxes, dropdowns, radio buttons, etc. We are using these kinds of forms and web pages daily. Selenium is a widely used testing framework that delivers automated testing of web applications. Selenium supports various browsers like Chrome, Firefox, Safari, and Internet Explorer. Attending online training for selenium will provide basic knowledge about selenium. In that way, dropdowns are a powerful feature of web applications and are available in various types like single-select or multi-select. Selenium provides us with a class named ‘select’ to handle dropdowns. The select class will provide various methods to work with dropdowns. Let us understand what dropdown is and how we can handle it with selenium. 

Dropdown in selenium: 

Dropdowns are used in most web forms for authentication and application or request submission, etc. It allows users to select options from the list. Single-select dropdown will help you to choose the appropriate option, whereas multi-select will allow you to choose more than one option from the list. So, to provide a dropdown in your form or web application, selenium offers users multiple methods associated with the ‘select’ class. Mostly dropdown is used for selecting and deselecting purposes in a web application. The value of the selected element will be called by value property.

Ways to select an option from the dropdown menu:  

Selenium provides three different ways to select an option from the dropdown menu. The methods are mentioned below:

  1. selectByIndex – The provided list contains an index starting from 0. You can choose an option based on that index. For example, if you wanted to select the 2nd element in a dropdown list, you can use selectByIndex(1). Since the list elements index starts from 0, the second element will get an index of
  2. selectByValue – This method allows the user to select an option based on its value attribute. For example, if you have a dropdown menu with options “white,” “blue,” or “red,” These options have value attributes ‘w,’ ‘b,’ and ‘r,’ respectively. To choose white, you need to use selectByValue(“w”).
  3. selectByVisibleText – This method allows the user to choose an option based on the visible text. For instance, if you have a dropdown menu with “Tamil,” “English,” or “French,” To choose English, you would use selectByVisibleText(“English”).

Selecting multiple options from the dropdown menu:

It is easy to find whether the dropdown will allow the user to select multiple values. The select class will provide a method called isMultiple() to verify whether the dropdown menu is single-select or multi-select. If the <select> tag contains multiple attributes, it allows the user to make multiple choices—the select tag with multiple attributes can be seen in the following picture.

You can use any of the methods discussed earlier to choose a single option or multiple options. By invoking the methods multiple times, you can choose different options many times.

Deselecting the options from the dropdown: 

Just like selecting the options, you can also deselect the values from the dropdown menu. But, deselecting the values is only applicable for the multi-select dropdown. You can change the selected option from one to another. The select class provides the following methods to do the deselecting process.

  1. deselect all () – This method allows the user to clear all the selected options from the entire dropdown. If you want to clear the selected options, you can use this.
  2. deselctByIndex() – Similar to the selectByIndex(), this method allows the user to deselect the option using its index. Below given is the command for deselecting using the index.

   deselctByIndex(int arg0): void

The command will deselect the option at index1.

  1. deselectByValue() – To deselect the option by using its value. Following is the command used to do this process. 

 deselectByValue(String arg0): void

4. deselectByVisbleText() – It allows the user to deselect the option using the text. The syntax is given below. If a few options are already selected in the dropdown list, you can select a particular option using this index.

 deselectByVisbleText(String arg0): void  

Wrapping up: 

This article will give you a basic understanding of dropdown and how it works with selenium. If you have any challenges in understanding it better, learn selenium training online to learn about it and its application.