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...
7 Answers 7
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");
Comments
Try to use :-
se.selectByValue("Male");
OR
se.selectByVisibleText("Man");
OR
Use javascriptexecutor
Comments
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);
Comments
driver.findElement(By.name("Sex[0]")).sendKeys("Man");
Finally i have found Solution Thanks everyone...
Comments
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
Comments
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");
Comments
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");