Saurabh Gupta
Saurabh Gupta

Reputation: 139

How to select and get dropdown value in Selenium WebDriver using Java

HTML Code is

<select class="form_input_select bx-def-font" name="Sex[0]">
     <option value="Male">Man</option>                 
  <option value="Female">Woman</option>                   
<option value="Other" selected="selected">_Other</option>
    </select>

I am using below Selenium Java code:

Select select = new Select(driver.findElement(By.name("Sex[0]")));
select.selectByIndex(0);
Thread.sleep(2000);

Cursor move on Man , But Man is not Show , Show only _Others

Please help me for solve my issues I have applied more and more syntax but but I am not success to show Man...

Upvotes: 2

Views: 19265

Answers (7)

YaDav MaNish
YaDav MaNish

Reputation: 1352

You can use the below code. Just try atleast whether the selected option is getting printed on the console

    Select select = new Select(driver.findElement(By.name("Sex[0]")));
    select.selectByIndex(0);
    
    WebElement element = select.getFirstSelectedOption();
    System.out.println(element.getText());
                  or
    select.selectByVisibleText("Man");

Upvotes: 0

user6260412
user6260412

Reputation: 21

You can use the below Code for Selecting the Values from Dropdown.

We have created the anonymous abject for Select.

new Select(driver.findElement(By.id("mainOrderForm:orderType"))).selectByVisibleText("Factory Order");

                        OR

new Select(driver.findElement(By.id("mainOrderForm:orderType"))).selectByIndex(Index_No.);

                        OR

new Select(driver.findElement(By.id("mainOrderForm:orderType"))).selectByValue("Value");

Upvotes: 0

k.s. Karthik
k.s. Karthik

Reputation: 761

To select any option from dropdown we have to click the dropdown element and select the desired option. Please find the below sample code:

WebElement gender = driver.findElement(By.name("Sex[0]"));
gender.click();
Select selectGender = new Select(gender);
selectGender.selectByValue("Male");
// or
// you can use any of below functions of Select class
selectGender.selectByIndex(0);
// or
selectGender.selectByVisibleText("Male");

Hope this helps

Upvotes: 0

Saurabh Gupta
Saurabh Gupta

Reputation: 139

driver.findElement(By.name("Sex[0]")).sendKeys("Man");

Finally i have found Solution Thanks everyone...

Upvotes: 0

Rajnish Kumar
Rajnish Kumar

Reputation: 2938

Hi think before selecting any option from the drop down please make all the options visible in the DOM so do it like below.

driver.findElement(By.xpath("path to drop down upon click it will show 
the dd with values")).click();

Now once the options are visible on the page use the way you select an option form the DD

Select se=new Select(driver.findElement(By.name("Sex[0]")));
se.selectByIndex(0);
Thread.sleep(2000);

Upvotes: 0

gihan-maduranga
gihan-maduranga

Reputation: 4801

you can use getText() to get selected text.

Select se=new Select(driver.findElement(By.name("Sex[0]")));
WebElement option = se.getFirstSelectedOption();
String gender=option.getText;

or use one of following options

se.selectByVisibleText("Man");
se.selectByIndex(0);
se.selectByValue("Male");

Upvotes: 1

Shubham Jain
Shubham Jain

Reputation: 17553

Try to use :-

se.selectByValue("Male");

OR

se.selectByVisibleText("Man");

OR

Use javascriptexecutor

Upvotes: 0

Related Questions