Handle Drop Down in Selenium

You will learn, How to handle the Drop down in Selenium. How can you select the Option from drop down in Selenium or How can you select the multiple options from drop down in Selenium? In Selenium, we can manage the Drop-Down using ‘Select’ class.

 

The Select class provides useful methods for interacting with drop-down options. Using Select class we can perform select and deselect operations. Using Select class we can also perform Multiple Select operations.

As Select is the class in Selenium. So we need to create the object of this class before use. Let’s see the methods of the Select class and perform some operation Drop-down list.

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

Before exploring the Select Class methods, First, let’s see the HTML structure of Dropdown. In HTML we can, there is index associated with each value in the drop-down.

Drop Down in Selenium

Now we will see the different Select methods of ‘Select Class’.

 

selectByIndex() in Selenium:

This method is used to Select the option based on the index given by the user.There is an attribute called “values” which will have the index values.

Let’s select the 6 index value in Month drop-down on the Facebook signup page.

The above code will select the month at index 6 in the Month drop-down field on facebook signup page.

 

selectByValue() in Selenium:

This method is used to select the dropdown value using HTML value tag. It will Select the options that have a value matching with the given argument by the user.

Let’s understand this with the example.

 

Suppose HTML code

<html>
<head>
<title>Select Example by Value</title>
</head>
<body>
<p>Which mobile device do you like most?</p>
<select name=”Mobiles”><option selectd> Please select</option>
<option value=”iphone”>iPhone</option>
<option value=”nokia”>Nokia</option>
<option value=”samsung”>Samsung</option>
<option value=”htc”>HTC</option>
<option value=”blackberry”>BlackBerry</option>
</select>
</body>
</html>

 

Selenium Code to on the basis of Above HTML structure

The above code selects the HTC in drop-down because HTML value for this is ‘htc’, What we are calling in our code.

 

selectByVisibleText() in Selenium:

To Select all options that display text matching the given argument in the method. It will not look for any index or value, it will try to match the VisibleText.

Let’ select ‘Sept’ in month field on the Facebook signup page.

The above code will select the ‘Sept’ month in the month drop-down field on facebook signup page.

 

Print All values of Drop-down in Selenium

To print or get all options of the drop-down, We need to use getOptions() method of the select class.

Sometimes you may like to count the element in the dropdown and multiple select boxes so that you can use the loop on Select element.

 

Let’s count the available values in Month Drop-down field on Facebook signup page and print these.

 

Deselect Methods –

Like select options in Drop-Down, we can Deselect the options as well. But the only difference is, We can use deselect with Multi-select options only.

1. deselectByIndex() – This method is used to Deselect the option at the given index.

2. deselectByValue() – This method is used to Deselect all options that have a value matching the given argument.

3. deselectByVisibletext() – This method is used to Deselect all options that display text matching the given argument.

4. deselectAll() – This method is used to Clear all selected entries. This works only when the SELECT supports multiple selections. It throws NotImplementedError if the “SELECT” does not support multiple selections.

0 Comment

Leave a Reply