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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20138761/
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-08-12 23:26:57  来源:igfitidea点击:

How to select a dropdown value in Selenium WebDriver using Java

javaselenium-webdriver

提问by testing

I am new to selenium , currently am working on selenium webdriver i want to select a value from the drop down. The id=periodId and the option is many in that am trying to select Last 52 weeks.

我是 selenium 的新手,目前正在研究 selenium webdriver 我想从下拉列表中选择一个值。id=periodId 和选项很多,我试图选择过去 52 周。

Here is the HTML code:

这是 HTML 代码:

<select id="periodId" name="period" style="display: none;">
    <option value="l4w">Last 4 Weeks</option>
    <option value="l52w">Last 52 Weeks</option>
    <option value="daterange">Date Range</option>
    <option value="weekrange">Week Range</option>
    <option selected="" value="monthrange">Month Range</option>
    <option value="yeartodate">Year To Date</option>
</select>

Please suggest me some ways to click the drop down.

请给我一些点击下拉菜单的方法。

I tried with the above example lines but am getting error such as Element is not currently visible and so may not be interacted with Command duration or timeout: 32 milliseconds the drop downs values are the jquery multiselect widget format.

我尝试使用上述示例行,但出现错误,例如 Element 当前不可见,因此可能无法与命令持续时间或超时交互:32 毫秒下拉值是 jquery 多选小部件格式。

采纳答案by Abhishek Singh

Just wrap your WebElement into Select Object as shown below

只需将您的 WebElement 包装到 Select Object 中,如下所示

Select dropdown = new Select(driver.findElement(By.id("identifier")));

Once this is done you can select the required value in 3 ways. Consider an HTML file like this

完成此操作后,您可以通过 3 种方式选择所需的值。考虑这样的 HTML 文件

<html>
<body>
<select id = "designation">
<option value = "MD">MD</option>
<option value = "prog"> Programmer </option>
<option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>

Now to identify dropdown do

现在识别下拉菜单

Select dropdown = new Select(driver.findElement(By.id("designation")));

Select dropdown = new Select(driver.findElement(By.id("designation")));

To select its option say 'Programmer' you can do

要选择其选项,请说“程序员”,您可以

dropdown.selectByVisibleText("Programmer ");

dropdown.selectByVisibleText("Programmer ");

or

或者

dropdown.selectByIndex(1);

dropdown.selectByIndex(1);

or

或者

 dropdown.selectByValue("prog");

回答by SamK

If you want to write all in one line try

如果您想在一行中写下所有内容,请尝试

new Select (driver.findElement(By.id("designation"))).selectByVisibleText("Programmer ");

回答by user4272927

Try this-

尝试这个-

driver.findElement(By.name("period")).sendKeys("Last 52 Weeks");

回答by AugustRush

As discussed above, we need to implement Select Class in Selenium and further we can use various available methods like :- enter image description here

如上所述,我们需要在 Selenium 中实现 Select Class,并且我们可以进一步使用各种可用的方法,例如:- 在此处输入图片说明

回答by Mohan Kumar Dg

code to select dropdownusing xpath

使用xpath选择下拉列表的代码

Select select = new 
Select(driver.findElement(By.xpath("//select[@id='periodId']));

code to select particaular optionusing selectByVisibleText

使用 selectByVisibleText选择特定选项的代码

select.selectByVisibleText(Last 52 Weeks);

回答by user5027588

Actually selectdoes select but not placing the selected values to the respective field . Where wondered the below snippet works perfectly

实际上select确实选择但不将选定的值放置到相应的字段中。哪里想知道下面的代码段完美无缺

driver.findElement(By.name("period")).sendKeys("Last 52 Weeks");

回答by Abhishek M S

WebDriver driver = new FirefoxDriver();
WebElement identifier = driver.findElement(By.id("periodId"));
Select select = new Select(identifier);
select.selectByVisibleText("Last 52 Weeks"); 

回答by ajayv

I have not tried in Selenium, but for Galen test this is working,

我还没有在 Selenium 中尝试过,但是对于 Galen 测试来说这是有效的,

var list = driver.findElementByID("periodID"); // this will return web element

list.click(); // this will open the dropdown list.

list.typeText("14w"); // this will select option "14w".

var list = driver.findElementByID("periodID"); // 这将返回 web 元素

列表。点击();// 这将打开下拉列表。

list.typeText("14w"); // 这将选择选项“14w”。

You can try this in selenium, the galen and selenium working are similar.

你可以在 selenium 中试试这个,galen 和 selenium 的工作原理是相似的。

回答by Vishnu More

First Import the package as :

首先将包导入为:

import org.openqa.selenium.support.ui.Select;

导入 org.openqa.selenium.support.ui.Select;

then write in single line as:

然后单行写成:

new Select (driver.findElement(By.id("sampleid"))).selectByValue("SampleValue");

new Select (driver.findElement(By.id("sampleid"))).selectByValue("SampleValue");

回答by Jayant Gupta

You can use following methods to handle drop down in selenium.

您可以使用以下方法来处理 selenium 中的下拉列表。

  1. driver.selectByVisibleText("Text");
  2. driver.selectByIndex(1);
  3. driver.selectByValue("prog");
  1. driver.selectByVisibleText("Text");
  2. driver.selectByIndex(1);
  3. driver.selectByValue("prog");

For more details you can refer http://www.codealumni.com/handle-drop-selenium-webdriver/this post.

有关更多详细信息,您可以参考http://www.codealumni.com/handle-drop-selenium-webdriver/这篇文章。

It will definately help you a lot in resolving your queries.

它肯定会帮助你解决你的查询很多。