How to Retrieve the values from a drop down using selenium-WebDriver?

I am using below script to retrieve specific item from drop down but it’s not working.

Select  salTitle =  new Select(driver.findElement(“s2id_QuickEnquiryForm_salutation”)));

showing below Error in console “Exception in thread “main” org.openqa.selenium.suppo

How to Retrieve the values from a drop down using selenium-WebDriver?

rt.ui.UnexpectedTagNameException: Element should have been “select” but was “div””

I am using selenium + javascript.can anyone here suggest a method to select an item from a dropdown menu.

please find the Below HTML,Source code for your reference!!

please find the attached image for HTML code of Specific to that field.

I welcome any input!

Thanks in advance!!

Train Asked on December 29, 2014 in Selenium WebDriver.

This is a very beautifully asked question. Thanks Uday!

on December 31, 2014.
Add Comment
  • 4 Answer(s)

    Hi Uday,

    DropDown means we select value from the dropdown, and every dropdown will be having “select” tag. You are getting  “select” but was “div”” error because, the tag is “div” instead “select”.

    And Dropdown and serach/text fields are different. Your object is text field but NOT dropdown field.

    Ex: See below code and you can see the select tag from this use can select the dropdown value. URL:

    1. <select id=”month class=”_5dba name=”birthday_month>
    2. <option selected=”1 value=”0>Month</option>
      <option value=”1>Jan</option>
      <option value=”2>Feb</option>
      <option value=”3>Mar</option>
      <option value=”4>Apr</option>
      <option value=”5>May</option>
      <option value=”6>Jun</option>
      <option value=”7>Jul</option>
      <option value=”8>Aug</option>
      <option value=”9>Sep</option>
      <option value=”10>Oct</option>
      <option value=”11>Nov</option>
      <option value=”12>Dec</option>
    3. </select>

    Hope you got the answer.

    Vice Professor Answered on December 30, 2014.

    Hi Sachin,

    Thanks for your reply..I have already checked Facebook home page  they are using  Select.  but the issue is  we have  div  tag instead of Select.

    in our over all  application we are using div only to select values from drop down.

    if you have any idea regarding  how to work with div, can you please suggest valuable thoughts. if you want i will share my Source code page also.

    Thanks in Advance.

    Thanks & Regards,
    Uday Reddy.S

    on December 30, 2014.

    Hi Uday,

    As per your question and error message I gave my suggestions.

    Note: Please see your code and error message.

    As per your Comment I came to know what is the issue is 🙂 .

    Please give me sample url please let me try 🙂 .

    And Uday, see below question and it is related or similar i think.

    on December 31, 2014.
    Add Comment

    Looking for this answer.. (Ctrl+F). Same issue here..

    Default Answered on December 31, 2014.
    Add Comment

    Hi Uday,

    Did this not work for you?


    Professor Answered on December 31, 2014.

    yes virender..The above mentioned link not working for me.
    I want for div tag not for  select tag

    on December 31, 2014.
    Add Comment

    Hi Uday

    If you have all drop down with div , you can simply right your own wrapper around it code would be something like below

    public static boolean selectDivDropDown(WebElement element,String matchingText){;
    List<WebElement> dropDownList = element.findElements(By.tagName("a"));
    for(WebElement dropDown:dropDownList){
    return true;
    throw new IllegalArgumentException("Not able to find Element with Text:"+matchingText+"inside the WebElement:"+element.toString());

    Please note this is just example , I have not checked the exact html and neither have tested this code , but you can take it forward from there

    Supporter Answered on January 2, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.