Part 2: Handling Single Select Drop-down Created Using SELECT Tag

Hello Folks,

In last post we have learnt basics of drop-down created using SELECT tag. Refer post here.

In this post, we will see how to handle single select drop-down.

We will be using below html code for this post:

When we open above html file in a browser, you will get drop-down as shown below:




Let’s see frequent operations performed on single select drop-down:

How to print all options available in drop-down:

  • Select class provides a method named getOptions() to get all available options in a drop-down.
  • Return type of getOptions() method is a List<WebElement>. Reason of returning List is a drop-down may have duplicate values as well. So to retrieve all options, selenium developers use List as a return type of getOptions() method.
  • After getting List<WebElement>, you need to iterate elements of List and use getText() method to get option value.

Code:

Output:

All options are printed below:
Select
Hindi
English
Kannada
Tamil
Telgu
Malaylam

How to select a value from drop-down:

We can select a value from drop-down using three ways:

  1. Using index. (Index starts from zero.).
  2. Using value.
  3. Using visible text.

If you see above html code, every option has default index value which is order of their occurrence. For example: First OPTION tag has index zero , second OPTION tag has index one and so on.

If you see above html code carefully, you will find every OPTION tag has a value attribute. Value attribute is optional. If it is available, We can use this also to select from a drop-down. Value can be any thing. For example: Value of OPTION tag which has inner text as Hindi can be anything other than Hindi also.

<option value=”Mother Tongue”>Hindi</option>

Inner text of OPTION tag is called as visible text. This can also be used to select value from drop-down.

We will see a sample program here:

If you run the above program, you will see first Hindi will be selected followed by Kannada and English. I have given sleep of 2 seconds to make selection visible to you. Remember, it is single select drop-down. So whenever we will call selectByXXX method, new value will be selected and previously selected value will be deselected automatically.

How to get selected option from drop-down:

We can retrieve selected option from drop-down using getFirstSelectedOption() method which returns a WebElement. You need to use getText() method on returned WebElement to get selected value.

Java code:

Output:
Selected value is: English

There is another way to get selected option as well by using getAllSelectedOptions() method which returns a List<WebElement>. Since we are calling this method on single select drop-down, we will get only one element in List. This method is mainly for multi select dropdown.

Java code:


Output:
Selected value is: English

What will happen when we pass wrong index/value/visible text?

When we pass wrong index/value/visibletext, we will get “org.openqa.selenium.NoSuchElementException”.

Try below code and check output:

//Out of index value

selectObject.selectByIndex(10);

// adding spaces
selectObject.selectByValue(” Kannada “);

// adding spaces
selectObject.selectByVisibleText(” English “);

How to deselect a selected value from drop-down?

We do not have any method which can deselect the selected option in single select drop-down. Either you need to select any other option or default option using selectByXXX() methods.

Complete Java code:

That’s all for single select drop-down operations. If you want to know for any other operation on single select drop-down, please let me know. I will add in this post.

If you have any doubt, feel free to ask here.

If you like my posts, please like, comment, share and subscribe.

#ThanksForReading

#HappySelenium




4 thoughts on “Part 2: Handling Single Select Drop-down Created Using SELECT Tag

  1. Hi Amod,

    One question,

    if there is a requirement to select disable element from a drop down how can we do this.

    yyy

    We can handle this using selenium?

    1. Hello,

      If you can select disable element manually then only you can select through selenium. Generally there is no meaning of selecting disable element in dropdown.
      Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *