2

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

Ripon Al Wasim
37.9k42 gold badges159 silver badges179 bronze badges
asked May 2, 2016 at 7:23

7 Answers 7

1

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");
answered May 2, 2016 at 7:32

Comments

0

Try to use :-

se.selectByValue("Male");

OR

se.selectByVisibleText("Man");

OR

Use javascriptexecutor

answered May 2, 2016 at 7:33

Comments

0

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);
answered May 2, 2016 at 7:54

Comments

0

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

Finally i have found Solution Thanks everyone...

answered May 2, 2016 at 8:01

Comments

0

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

answered May 2, 2016 at 10:52

Comments

0

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");
DanielBarbarian
5,37312 gold badges37 silver badges45 bronze badges
answered May 4, 2016 at 6:00

Comments

0

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");
answered Nov 22, 2020 at 19:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.