• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / How to Handle Dropdown in Selenium webdriver

How to Handle Dropdown in Selenium webdriver

January 8, 2019 by Mukesh Otwani 66 Comments

Dropdown in Selenium webdriver

Hello, Guys, Welcome to Selenium tutorial, today in this post we will see how to handle Dropdown in Selenium webdriver.

Generally, while working with the script we have to select some values from the dropdown and do other activity and validate. The dropdown is just like simple WebElement like Checkbox, textbox etc.

 

Before moving to Dropdown in Selenium webdriver check out few useful posts that will help you while writing scripts.

Dynamic XPath in Selenium

Xpath Plugin for Chrome and ChroPath for Chrome for XPath

 

Please refer the video for the same.

 

 

 

 

Dropdown in Selenium webdriver

Select values from Dropdown in Selenium webdriver

For handling dropdowns, Selenium already provides Select class that has some predefined method which help is a lot while working with Dropdown.

Select class can be find in below package.

org.openqa.selenium.support.ui.Select

 

Select values Dropdown in Selenium webdriver

Let’s Discuss some of the methods and will see the detailed program as well.

  • Select value using Index.

WebElement month_dropdown=driver.findElement(By.id(“month”));

Select month=new Select(month_dropdown);

 month.selectByIndex(4);

Explanation- Here selectbyIndex(int) is method which accept integer as argument so depends on index it will select values. If you give index as 4, it will select 5th value.

Note- Index will start from 0.

 

  • Select value using value attribute.
WebElement month_dropdown=driver.findElement(By.id("month"));

 Select month=new Select(month_dropdown);

 month.selectByValue(“5”);

Explanation- Here selectByValue(String) is a method which accepts values it means whatever value you have in your dropdown. Please refer below screenshot to see how to get values from the dropdown.

In our example we have taken value as 5 it means it will select May from dropdown.

 

  • Select value from Visible text
WebElement month_dropdown=driver.findElement(By.id("month"));

 Select month=new Select(month_dropdown);

 month.selectByVisibleText("Aug");

Explanation- We can also select the value from the text as well. This is very straight forward that whatever text we are passing it simply select that value.

Note- This is case sensitive it means if I pass Aug and dropdown has aug then Selenium will not be able to select value and it will fail your program so make sure Text which you are passing is correct.

In the above example, I am passing Aug in the argument so it will select aug from the dropdown.

 

  • Get Selected option from Dropdown.
WebElement month_dropdown=driver.findElement(By.id("month"));

 Select month=new Select(month_dropdown);

 WebElement first_value=month.getFirstSelectedOption();

 String value=first_value.getText();

Explanation- Here getFirstSelectedOption() will return the first selected option and will return you the WebElement then you can use getText() method to extract text and validate the same based on your requirement

 

  • Get All option from dropdown
WebElement month_dropdown=driver.findElement(By.id("month"));

Select month=new Select(month_dropdown);

List<WebElement> dropdown=month.getOptions();

 for(int i=0;i<dropdown.size();i++){

 String drop_down_values=dropdown.get(i).getText();

 System.out.println("dropdown values are "+drop_down_values);

 }

Explanation- getOptions() is a method of Select class which will return List of WebElement then we can iterate using for loop or iterator and using getText() method we can extract values.

I have another very useful video which will show you how you can handle dropdown which does not have any Select class as well.

This is called BootStrap Dropdown and you can handle them using the findElements method. I have created a video for the same.

Hope you have enjoyed the article and videos. If yes then do share with your friends and let me know if any help required from my side.

More updates Learn Automation page

For any query join Selenium group- Selenium Group

Filed Under: Basic Selenium Tagged With: Dropdowns

Reader Interactions

Comments

  1. padma kacham says

    October 8, 2021 at 8:32 AM

    How to write a code for div tags of drop down

    Reply
    • Mukesh Otwani says

      October 8, 2021 at 9:46 AM

      Hi Padma,

      You can use the approach mentioned in https://learn-automation.com/handle-bootstrap-dropdown-in-selenium-webdriver/. Select class from Selenium will work whenever

      Reply
  2. Revathi says

    November 11, 2020 at 5:20 PM

    Hi Mukesh,

    How to select the value (string) from drop down list using selenium.
    I used “Selectfromdropdownbyvalue”. but it fails.kindly help me

    Reply
    • Mukesh Otwani says

      November 12, 2020 at 11:16 PM

      Hi Revathi,

      Are you sure that you are dealing with the

      Reply
  3. Vishal Gupta says

    October 28, 2020 at 2:55 PM

    Hi , mukesh i am getting Element could not be scrolled into view exception, i dont know how to use scrolling inside a select tag i have tried and searched so much on internet but not getting exactly what i want, my drop down contains a scroll option .

    Reply
    • Mukesh Otwani says

      October 28, 2020 at 6:03 PM

      Hey Vishal, can you please share your code so that I can guide you?

      Reply
  4. Parth says

    October 22, 2020 at 6:13 PM

    Hello admin i got this error in eclipse console
    “Element should have been “select” but was “div””

    Here below is my code for dropdownlist (i used xpath because there is nothing show in element)
    WebElement location = driver.findElement(By.xpath(“//*[@id=\”WorkOrderModal_outlet_id\”]/div/div/div[1]”));
    Select locations = new Select(location);
    locations.selectByIndex(2);
    }

    Reply
    • Mukesh Otwani says

      October 26, 2020 at 1:47 PM

      Hi Parth,

      Select class only works when you have dropdown with Select tag. If you have any other tag then follow the below approach. I have a detailed video on this too http://learn-automation.com/handle-bootstrap-dropdown-in-selenium-webdriver/

      Reply
  5. Chetan Sonawane says

    May 20, 2020 at 11:10 AM

    Hi Mukesh,

    I have a scenario where few more options are added in the country dropdown like (added 10 more countries), Could you please help me how to verify that newly added options are available in the dropdown ?

    Your help is much appreciated !!

    Thanks in advanced 🙂

    Reply
    • Mukesh Otwani says

      May 21, 2020 at 8:33 AM

      Hi Chetan,

      Once you get getAllOptions from dropdown in a list, verify new values being added.

      Reply
  6. Ranjeet says

    June 28, 2019 at 8:26 AM

    hai Mukesh ,i want to print the value of the selected option by index from drpdown

    Reply
    • Mukesh Otwani says

      June 28, 2019 at 10:06 AM

      Hi Ranjeet,

      Use selectClassObject.getFirstSelectedOption().getText() and this returns String which you can print on console using sysout.

      Reply
  7. dee says

    May 9, 2019 at 2:27 PM

    we can also make use of the for loop in below way,

    //monthdropdown handling
    WebElement month_dropdown = driver.findElement(By.id(“month”));
    Select month_dd=new Select(month_dropdown);

    //fetching total no.of options present in dropdown
    List getvaluesmonth_dd=month_dd.getOptions();

    //printing total no using size()
    int totalvaluesinmonth_dd=getvaluesmonth_dd.size();
    System.out.println(“total options in month dropdown is “+totalvaluesinmonth_dd);

    for(int i=0;i<getvaluesmonth_dd.size();i++){
    System.out.println(getvaluesmonth_dd.get(i).getText());
    }

    Reply
    • Mukesh Otwani says

      May 9, 2019 at 8:02 PM

      Hi Dee,

      This way also it works…:)

      Reply
  8. neelu says

    April 22, 2019 at 12:39 PM

    how to select the option from cascading dropdown in selenium webdriver using java code?

    Reply
    • Mukesh Otwani says

      April 23, 2019 at 12:04 AM

      Hi Neelu can you share application URL where I can find cascading dropdown ?

      Reply
  9. nayana says

    April 20, 2019 at 9:25 PM

    hi
    could please help me as I have to prepare an interview as an automation tester.
    please advise which part need to more focus.

    Reply
    • Mukesh Otwani says

      April 23, 2019 at 12:34 AM

      Hi Nayana, this will help http://learn-automation.com/selenium-interview-questions-and-answers/

      Reply
  10. Rahul says

    March 26, 2019 at 8:26 PM

    The chrome driver clicks on the wrong element when the browser zoom level is not set to 100%, how can i solve this issue ?

    Reply
    • Mukesh Otwani says

      March 27, 2019 at 9:49 AM

      Hi Rahul,

      Its always recommendable to have 100% zoom setting for all browsers while using Selenium. There is a limitation for this. Please check this Chromium Bug where same bug has been posted log back into chromium project

      Reply
  11. Saiprasad says

    February 25, 2019 at 5:04 PM

    Hi Mukesh,

    How to do the select values from an excel. Suppose that i am writing a code for a form for which i am taking values from an excel. Now how to deal with select method. How to write a program for select method which would select a value based on the excel data

    Reply
    • Mukesh Otwani says

      February 27, 2019 at 10:00 PM

      Hi Sai,

      Whatever value you want to set in drop down, read same data from excel. You just need to pass value from excel as argument to method which contains select operation.

      Reply
  12. sucheta says

    January 25, 2019 at 3:23 PM

    How to select element from drop down for label class

    Reply
    • Mukesh Otwani says

      January 27, 2019 at 1:48 AM

      Hi Sucheta,

      If there is no select tag then obviously Selenium won’t provide native support for such kind of drop down. You need to come up with your custom way to handle such kind of control. You can refer this link for better understanding http://learn-automation.com/handle-bootstrap-dropdown-in-selenium-webdriver/

      Reply
  13. Chedomil says

    February 15, 2017 at 7:41 PM

    Hello Mukesh,
    How can I select last value in in dropdown, when I do not know final index?
    This option would be perfect, but…
    abc.selectByIndex(max index)
    or
    abc.selectByValue(contains “something”)
    abc.getLastSelectedOption 🙂
    Do you have any advice about this?

    Reply
    • Mukesh Otwani says

      February 16, 2017 at 11:48 AM

      Hi Chedomil,

      You can call getAllOptions().size()-1 which will always select last value from Dropdown.

      Reply
      • Bapaiah says

        July 26, 2019 at 8:02 AM

        HI Mukesh’

        it’s getoptions().size()-1 or getAllOptions().size()-1 .. pls correctme

        Reply
        • Mukesh Otwani says

          July 26, 2019 at 9:54 AM

          Hi Bapaiah,

          As per selenium documentation,
          getOptions Returns: List of all options belonging to this select tag
          getAllSelectedOptions Returns: List
          of all selected options belonging to select tag

          Reply
  14. anita says

    February 8, 2017 at 1:07 PM

    hi
    how to read first element from dropdown

    Reply
    • Mukesh Otwani says

      February 8, 2017 at 2:55 PM

      Hi Anita,

      Use getOptions(), this returns list of WebElements. Then you can get first index followed by getText() to fetch exact value which you can see in webpage.

      Reply
  15. Srihari Babu Anaparthi says

    February 4, 2017 at 6:56 AM

    Hi Mukesh,

    I am trying to select dropdown value using selectByVisiabletext option but it is not working IE9 Browser.Could you pls help me.

    Thanks,
    Srihari

    Reply
    • Mukesh Otwani says

      February 4, 2017 at 10:43 AM

      Hi Srihari,

      First please share an executable scenario and a sample page to look into this issue.

      Reply
  16. Armands says

    January 13, 2017 at 9:13 PM

    Hi Mukesh,
    Hi Anil,

    I face to problem. When the dropdown tag element is not a ‘select’ but a ‘button’

    Reply
    • Mukesh Otwani says

      January 13, 2017 at 10:03 PM

      Hi Armands,

      Selenium by default supports only select tag. If you are seeing other than select then you need to go for some workaround.

      Reply
  17. Dhanalakshmi says

    December 24, 2016 at 5:24 PM

    Hi mukesh ,
    I have error in my code..
    plz resolve it

    FirefoxDriver driver=new FirefoxDriver();
    driver.get(“http://www.gmail.com”);

    File srcFile= driver.getScreenshotAs(OutputType.FILE);
    File destFile= new File(“D://image.png”);
    FileUtils.copyFile(srcFile, destFile);

    Reply
    • Mukesh Otwani says

      December 28, 2016 at 11:48 AM

      Hi,

      Kindly refer below post for screenshot in Selenium
      http://learn-automation.com/how-to-capture-screenshot-in-selenium-webdriver/

      Reply
  18. Dhanalakshmi says

    December 24, 2016 at 4:26 PM

    Hi Mukesh,
    Im getting error in this code. plz resolve it.
    FirefoxDriver driver=new FirefoxDriver();
    driver.get(“http://www.airindia.com”);
    Select select=new Select(driver.findElement(By.xpath(“.//*[@id=’_classType1′]”))Select.selectByValue(3));

    Reply
    • Mukesh Otwani says

      December 28, 2016 at 11:52 AM

      Hi,

      use selectByIndex(3)

      Reply
  19. Priyanka D. says

    November 24, 2016 at 11:17 AM

    Hi Mukesh,

    Plz help me on how to handle auto complete text boxes.

    Reply
    • Mukesh Otwani says

      November 24, 2016 at 5:30 PM

      Hi Priyanka,

      Below link will guide you for the same.

      http://www.software-testing-tutorials-automation.com/2014/05/how-to-handle-ajax-auto-suggest-drop.html

      Reply
  20. anusha says

    November 22, 2016 at 4:14 PM

    how to perform click automation in dropdown one by one in selenium webdriver

    Reply
    • Mukesh Otwani says

      November 22, 2016 at 4:26 PM

      Hey Anusha,

      You have 2 options to do this. You can get all dropdown using by tagname and then run a for loop to handle this.

      I have also recorded the video for another kind of dropdown which will give you clear picture.

      Reply
  21. Arjun says

    November 19, 2016 at 1:01 PM

    how to perform actions on already opened browser

    Reply
    • Mukesh Otwani says

      November 23, 2016 at 3:21 PM

      Hi Arjun,

      Selenium always starts with fresh session so you can’t perform operation on opened browser.

      Reply
  22. Radik says

    September 19, 2016 at 4:42 PM

    Hi Mukesh,
    Thanks you so much for your excellent tutorial.

    I wanted to ask how can I convert WebElement to string so I can use array list?
    I want to solve your assignment.
    Please assist.

    Thanks
    Radik

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 2:52 PM

      Hey Radik,

      What is the need to converting WebElement to String?

      Reply
  23. RC says

    September 14, 2016 at 11:20 AM

    Hi.

    I dont know what i am doing wrong. but my select is not working. iv tried facebook and many other demo sites. need help plz

    RC

    Reply
    • Mukesh Otwani says

      September 15, 2016 at 4:41 PM

      Hi Rupika,

      Can you send me your script to mukeshotwani@learn-automation.com I will check and reply.

      Reply
    • kashsih says

      February 19, 2017 at 6:11 PM

      hey even i am facing the same error. did u find out fault in ur code. kindly let me know

      Reply
      • Mukesh Otwani says

        February 21, 2017 at 5:29 PM

        Hi Kashish,

        Are you running your script from cmd?

        Reply
  24. Abhinav says

    September 10, 2016 at 5:14 PM

    Hi Mukesh

    I think the index does not start from 0. As I gave the index 2 and Feb got selected.
    Please cross check.

    Reply
    • Mukesh Otwani says

      September 12, 2016 at 4:18 PM

      Hi Abhinav,

      It starts with zero, In this case zero index points to month text

      Reply
  25. Somanath says

    September 1, 2016 at 10:59 AM

    hi Mukesh,
    how to store the expected values in arrayList and then how will using the assersion for this ArrayList

    Reply
    • Mukesh Otwani says

      September 2, 2016 at 5:59 PM

      Hi Somanath this is my next post will update soon.

      Reply
  26. shabana banu says

    August 7, 2016 at 11:47 AM

    Hi Mukesh

    I like your article and I am following it, but I am stuck with div class. I dont know how to select the item from div class drop down, please help me out.

    Thanks
    Shabana

    Reply
    • Mukesh Otwani says

      August 18, 2016 at 10:47 PM

      Hi Shabana,

      Below post covers the same http://selenium-venkat.blogspot.in/2016/02/21-select-month-from-birthday-field-in.html

      Reply
  27. Raju Raipure says

    August 4, 2016 at 2:32 PM

    Hi Mukesh,
    I have a drop down option which displays the options on mouse over. I want o get all the options available under the drop down. Can you please suggest how can I do it?
    Thanks

    Reply
    • Mukesh Otwani says

      August 5, 2016 at 10:34 AM

      Hi Raju, any sample app?

      Reply
  28. pavani says

    July 13, 2016 at 11:22 PM

    hi mukesh,

    If we are using drop down but having div class instead of select class what needs to be done

    Reply
    • Mukesh Otwani says

      July 14, 2016 at 11:07 AM

      Hi Pavani,

      In this case you can use findElements() method which will return list of elements then you can select the item.

      Reply
  29. Amit Chaudhary says

    May 21, 2016 at 10:46 AM

    Hi Mukesh,

    Thanks for your reply. My code is working fine now, i messed up with index value in my code.

    But now i have another problem related to radio buttons, there is page on which only 2 radio buttons are visible on application and 2 radio are hidden, but i want to select the first visible radio button. Is there any way i can do this ?

    below are steps you use to create a scenario-
    open link – https://www.fedex.com/ratefinder/home?cc=US&language=en&locId=express
    select *No. of packages as ‘2’ under additional information section
    here i want to select first radio button based on index rather than any other attribute.

    Thanks,
    Amit Chaudhary

    Reply
    • Mukesh Otwani says

      May 24, 2016 at 1:38 AM

      Hi Amit,

      Dynamic xpath will help you check dynamic xpath video in Youtube.

      Reply
    • Atul gupta says

      August 9, 2016 at 11:19 AM

      Hi Mukesh,

      I have seen you all videos. All are very helpful to me and i am enhancing my skill through this all.

      today i have a query now that If we have multiple dropdown and all dropdown is depend on previous one selection dropdown. How we will execute second one or how to write the script in webdriver.

      Ex:- one dropdrown for Mobile Brand name
      Second dropdown for Mobile Model so mobile model name depend on Brand name.
      Can you provide me solution as soon as possible please.

      Regard.

      Atul gupta

      Reply
      • Mukesh Otwani says

        August 18, 2016 at 10:10 AM

        Hi Atul,

        Thanks for nice feedback 🙂 I generally use explicit wait to handle this scenario.

        Reply
  30. Amit Chaudhary says

    May 19, 2016 at 11:32 PM

    Hi Mukesh,

    i’m trying to trying to perform select operation on a dropdown using index of the element. Something like this –

    For Example, here i’m writing below code to select some values from any dropdown from a page using index. But below code is not working, Can you please help me on this ?

    List el = driver.findElements(By.tagName(“select”));

    Select select = new Select(el.get(index));
    select.selectByVisibleText(value);

    Reply
    • Mukesh Otwani says

      May 21, 2016 at 1:16 AM

      Hi Amit,

      Which app you are automating?

      Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

Video Player
https://www.youtube.com/watch?v=w_iPCT1ETO4
00:00
00:00
31:21
Use Up/Down Arrow keys to increase or decrease volume.

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?