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

如何使用Java在SeleniumWebDriver中选择下拉值,java,selenium-webdriver,Java,Selenium Webdriver,我是selenium新手,目前正在使用selenium webdriver,我想从下拉列表中选择一个值。 id=periodId,选项很多,我试图选择最后52周 以下是HTML代码: <select id="periodId" name="period" style="display: none;"> <option value="l4w">Last 4 Weeks</option> <option value="l52w">Las

我是selenium新手,目前正在使用selenium webdriver,我想从下拉列表中选择一个值。 id=periodId,选项很多,我试图选择最后52周

以下是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>
请建议我一些单击下拉列表的方法

我尝试了上面的示例行,但出现了错误,例如元素当前不可见,因此可能无法与之交互 命令持续时间或超时:32毫秒
下拉列表值是jquery multiselect小部件格式。

只需将WebElement包装到Select对象中,如下所示

Select dropdown = new Select(driver.findElement(By.id("identifier")));
完成此操作后,您可以通过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>

如果你想把所有的内容都写在一行中,试试看

new Select (driver.findElement(By.id("designation"))).selectByVisibleText("Programmer ");
试试这个-

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

如上所述,我们需要在Selenium中实现Select类,而且我们还可以使用各种可用方法,如:- 使用xpath选择下拉列表的代码

Select select = new 
Select(driver.findElement(By.xpath("//select[@id='periodId']));
使用selectByVisibleText选择particaular选项的代码

select.selectByVisibleText(Last 52 Weeks);
“实际选择”会选择,但不会将选定值放置到相应字段中。下面的代码片段完美地工作在哪里


我没有试过硒,但对于Galen测试,这是有效的

var list=driver.findElementByIDperiodID;//这会回来的 web元素

list.click;//这将打开下拉列表

list.typeText14w;//这将选择选项14w


您可以在selenium中尝试这一点,galen和selenium工作原理类似。

首先将软件包导入为:

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

然后以单行形式写入:

新建选择驱动程序.findElementBy.idsampleid.selectByValueSampleValue


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

 1. driver.selectByVisibleText("Text");
 2. driver.selectByIndex(1);
 3. driver.selectByValue("prog");
有关更多详细信息,请参阅本文


它肯定会对您解决查询有很大帮助。

可能重复我尝试了链接方法它不适用于我的选项,我可以使用其他模式吗?我认为由于某种原因下拉列表不可见,更改它不是很好的方法,但是您可以始终使用Javascript更改元素的属性值这对于常规的Select使用来说是一个很好的答案,但是我认为真正的问题是Select元素不可见——通过导入的方式:import org.openqa.selenium.support.ui.Select;对于使用C Notes的用户,您必须安装NuGet中提供的Selenium支持才能使用此解决方案。同样在C语言中,它被称为SelectElement而不是Select。如果您知道要选择的值,那么即使是driver.findElementBy.xpathYour_Xpath_to_Element.sendKeySelected值也可以使用FireFox进行测试并正常工作good@RajeshBalan. 对但是,不建议使用XPath标识元素。如果DOM结构发生更改,自动化代码将失败。请解释您的答案。虽然此代码可能提供问题的解决方案,但强烈建议您提供有关此代码为什么和/或如何回答此问题的其他上下文。从长远来看,纯代码的答案通常会变得毫无用处,因为未来遇到类似问题的观众无法理解解决方案背后的原因。这对我来说不起作用。我使用了:driver.findElementBy.xpath//select[option[@value='50']].sendKeys50;
WebDriver driver = new FirefoxDriver();
WebElement identifier = driver.findElement(By.id("periodId"));
Select select = new Select(identifier);
select.selectByVisibleText("Last 52 Weeks"); 
 1. driver.selectByVisibleText("Text");
 2. driver.selectByIndex(1);
 3. driver.selectByValue("prog");