Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/08/2025 By admin

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. 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, 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.

Hindi

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. 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

Uncategorized

Post navigation

Previous Post: Part 5: Waits In Selenium: What Happens When We Mix Sleep With Other Types Of Waits ?
Next Post: Java Program To Find Closest Value Of A Given Number In Sorted Array

Related Posts

Selenium Topics – Page 21 – Make Selenium Easy Uncategorized
Make Selenium Easy – Page 46 of 47 – Uncategorized
Implementation of Explicit Wait as FluentWait and WebDriverWait in Selenium WebDriver Uncategorized
March 5, 2019 – Make Selenium Easy Uncategorized
Amod Mahajan, Author at Make Selenium Easy – Page 4 of 49 Uncategorized
October 1, 2018 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark