Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can I select an item from a drop down list like this gender(i.e male,female from dropdown) 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();   
}

but this didn't succeed.

share|improve this question
1  
Similar to: stackoverflow.com/questions/7232544/… – 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 – user1754106 Oct 17 '12 at 18:29
1  
Stackoverflow users helps those who help themselves :) – amey Oct 17 '12 at 19:14
add comment (requires an account with 50 reputation)

7 Answers

  1. This is a really bad title. (Edit: It has been improved)
  2. Google "select item selenium webdriver" brings up How do I set a 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. http://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.
share|improve this answer
I want using java.. can u help me out – user1754106 Oct 17 '12 at 18:50
Try to explain where you get stuck. Like: I don't know how to proceed because I really don't get this weired ruby syntax and in the python API the element has a select-method, but it's not available in Java. Then I might be able. But the answer by Maitreya looks good to me. If it works, don't forget to mark it as solution. – Mene Oct 18 '12 at 12:37
add comment (requires an account with 50 reputation)

Use -

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

You need to import org.openqa.selenium.support.ui.Select;

share|improve this answer
5  
This is a man's world, but it wouldn't be nothing, nothing without a woman or a german! – Mene Oct 18 '12 at 12:25
add comment (requires an account with 50 reputation)
 WebElement selectgender = driver.findElement(By.id("gender"));
 selectgender.sendKeys("Male");
share|improve this answer
Thanks for your answer, it worked for me. – Umamaheshwar Thota Feb 5 at 6:08
add comment (requires an account with 50 reputation)

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

share|improve this answer
add comment (requires an account with 50 reputation)

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.

share|improve this answer
add comment (requires an account with 50 reputation)
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();   
}
share|improve this answer
add comment (requires an account with 50 reputation)

public static void main(String[] args){

     WebDriver webdriver = new FirefoxDriver();
     webdriver.get("http://www.makemytrip.com");

       if(webdriver.findElement(By.id("oneway_r")).isSelected()){

Select leavingfrom = new Select(webdriver.findElement(By.xpath(".//*[@id='selorigin_s']"))); leavingfrom.selectByVisibleText("Hyderabad(HYD)");

     }
     else
         System.out.println("Please select the one way trip");

     }

Iam trying to select a value from dropdown box for URL:makemytrip but I am getting Error:Element should have been "select" but was "input" Can anyone post the actual result

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

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