如何使用 Java 在 Selenium WebDriver 中选择和获取下拉值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36977072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:03:51  来源:igfitidea点击:

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

javaseleniumselenium-webdriver

提问by Saurabh Gupta

HTML Code is

HTML 代码是

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

我在下面使用 Selenium Java 代码:

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

光标在 Man 上移动,但 Man 不显示,只显示 _Others

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

请帮助我解决我的问题我已经应用了越来越多的语法,但我没有成功向 Man 展示...

回答by gihan-maduranga

you can use getText()to get selected text.

您可以使用getText()来获取选定的文本。

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");

回答by Shubham Jain

Try to use :-

尝试使用:-

se.selectByValue("Male");

OR

或者

se.selectByVisibleText("Man");

OR

或者

Use javascriptexecutor

使用 javascriptexecutor

回答by eduliant

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

嗨,在从下拉列表中选择任何选项之前,请让所有选项在 DOM 中可见,因此请按照下面的方式进行操作。

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

现在,一旦选项在页面上可见,请使用您从 DD 中选择选项的方式

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

回答by Saurabh Gupta

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

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

Finally i have found Solution Thanks everyone...

最后我找到了解决方案谢谢大家...

回答by k.s. Karthik

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

希望这可以帮助

回答by user6260412

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

您可以使用以下代码从下拉列表中选择值。

We have created the anonymous abject for Select.

我们为 创造了匿名对象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");