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(By.id(“s2id_QuickEnquiryForm_salutation”)));
 salTitle.selectByVisibleText(“Mr.”);

showing below Error in console “Exception in thread “main” org.openqa.selenium.suppoHow 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!!

Uday 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: https://www.facebook.com/

 
  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.

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

http://forumsqa.com/question/how-to-select-jq-widget-drop-down-using-selenium-webdriver/

on December 31, 2014.
Add Comment

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

balukondamuri Default Answered on December 31, 2014.
Add Comment

Hi Uday,

Did this not work for you?

http://www.toolsqa.com/selenium-webdriver/dropdown-multiple-select-operations/

Thanks
Virender

Virender Singh 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){

element.click();

List<WebElement> dropDownList = element.findElements(By.tagName("a"));

for(WebElement dropDown:dropDownList){

if(dropDown.getText().equalsIgnoreCase(matchingText)){

dropDown.click();

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

kapilag Supporter Answered on January 2, 2015.
Add Comment

Your Answer

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