43

How can I select an item from a drop down list like gender (eg male, female) using Selenium WebDriver with Java?

I have tried this

WebElement select = driver.findElement(By.id("gender"));
List<WebElement> options = select.findElements(By.tagName("Male"));
for (WebElement option : options) {
    if("Germany".equals(option.getText()))
        option.click();   
}

My above code didn't work.

T J
  • 42,762
  • 13
  • 83
  • 138
  • 1
    Similar to: http://stackoverflow.com/questions/7232544/selenium-webdriver-and-dropdown-boxes – 3ck Oct 17 '12 at 18:14
  • sorry this couldn,t help me out.. can you provide me the way or give me any idea so i can proceed –  Oct 17 '12 at 18:29
  • 1
    Stackoverflow users helps those who help themselves :) – Amey Oct 17 '12 at 19:14
  • I wonder if you don't convert a select WebElement to Select, then could it be possible that selenium will click the wrong option ? See this post - http://stackoverflow.com/questions/40031592/python-selenium-webdriver-select-option-does-not-work – MasterJoe Oct 14 '16 at 23:57

9 Answers9

44

Use -

new Select(driver.findElement(By.id("gender"))).selectByVisibleText("Germany");

Of course, you need to import org.openqa.selenium.support.ui.Select;

Community
  • 1
  • 1
some_other_guy
  • 3,364
  • 4
  • 37
  • 55
  • 6
    Note that in C# the class is `SelectElement` instead of `Select`. Also it's not part of the core `Selenium.WebDriver` package, to use this class you also have to install the `Selenium.Support` package. – Luis Perez Oct 19 '15 at 18:40
  • Can you please tell us why we need to convert WebElement into a Select instance ? Is it only so that we can get Select specific methods which make it easy to automate select ? Also, can we automate a select, without using Select class ? – MasterJoe Oct 14 '16 at 23:44
23

Just wrap your WebElement into Select Object as shown below

Select dropdown = new Select(driver.findElement(By.id("identifier")));

Once this is done you can select the required value in 3 ways. Consider an HTML file like this

<html>
<body>
<select id = "designation">
<option value = "MD">MD</option>
<option value = "prog"> Programmer </option>
<option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>

Now to identify dropdown do

Select dropdown = new Select(driver.findElement(By.id("designation")));

To select its option say 'Programmer' you can do

dropdown.selectByVisibleText("Programmer ");

or

dropdown.selectByIndex(1);

or

dropdown.selectByValue("prog");

Happy Coding :)

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Abhishek Singh
  • 10,243
  • 22
  • 74
  • 108
5

Tagname you should mentioned like that "option", if text with space we can use this method it should work.

WebElement select = driver.findElement(By.id("gender"));
List<WebElement> options = select.findElements(By.tagName("option"));

for (WebElement option : options) {

if("Germany".equals(option.getText().trim()))

 option.click();   
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Sathish
  • 51
  • 1
  • 1
3

Google "select item selenium webdriver" brings up How do I set an option as selected using Selenium WebDriver (selenium 2.0) client in ruby as first result. This is not Java, but you should be able to translate it without too much work. https://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriver is in the top 5, again not Java but the API is very similar.

Mene
  • 3,739
  • 21
  • 40
3
WebElement selectgender = driver.findElement(By.id("gender"));
selectgender.sendKeys("Male");
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Anand Somani
  • 801
  • 1
  • 6
  • 15
3

You can use 'Select' class of selenium WebDriver as posted by Maitreya. Sorry, but I'm a bit confused about, for selecting gender from drop down why to compare string with "Germany". Here is the code snippet,

Select gender = new Select(driver.findElement(By.id("gender")));
gender.selectByVisibleText("Male/Female");

Import import org.openqa.selenium.support.ui.Select; after adding the above code. Now gender will be selected which ever you gave ( Male/Female).

Deepu
  • 113
  • 2
  • 16
1
WebElement select = driver.findElement(By.id("gender"));
List<WebElement> options = select.findElements(By.tagName("option"));
for (WebElement option : options) {
   if("Germany".equals(option.getText()))
       option.click();   
}
Anand Somani
  • 801
  • 1
  • 6
  • 15
1

To find a particular dropdown box element:

Select gender = new Select(driver.findElement(By.id("gender")));

To get the list of all the elements contained in the dropdown box:

for(int j=1;j<3;j++)
    System.out.println(gender.getOptions().get(j).getText());

To select it through visible text displayed when you click on it:

gender.selectByVisibleText("Male");

To select it by index (starting at 0):

gender.selectByIndex(1);
alvarobartt
  • 453
  • 5
  • 15
Rishi369
  • 27
  • 6
0
public class checkBoxSel {

    public static void main(String[] args) {

         WebDriver driver = new FirefoxDriver();
         EventFiringWebDriver dr = null ;


         dr = new EventFiringWebDriver(driver);
         dr.get("http://www.google.co.in/");

         dr.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

         dr.findElement(By.linkText("Gmail")).click() ;

         Select sel = new Select(driver.findElement(By.tagName("select")));
         sel.selectByValue("fil");

    }

}

I am using GOOGLE LOGIN PAGE to test the seletion option. The above example was to find and select language "Filipino" from the drop down list. I am sure this will solve the problem.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
MKod
  • 803
  • 5
  • 20
  • 33