如何使用Java在Selenium WebDriver中选择下拉列表值 [英] How to select a dropdown value in Selenium WebDriver using Java
问题描述
我是selenium的新手,目前正在研究selenium webdriver我想从下拉列表中选择一个值。
id = periodId和选项很多,我试图选择过去52周。
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.
这是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.
我尝试使用上面的示例行但是得到错误,例如Element当前不可见,因此可能无法与
命令持续时间或超时交互:32毫秒
下拉值是jquery multiselect小部件格式
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
推荐答案
将WebElement包装成Select Object,如下所示
Just wrap your WebElement into Select Object as shown below
Select dropdown = new Select(driver.findElement(By.id("identifier")));
完成后,您可以通过3种方式选择所需的值。考虑像这样的HTML文件
Once this is done you can select the required value in 3 ways. Consider an HTML file like this
<html>
<body>
<select id = "designation">
<option value = "MD">MD</option>
<option value = "prog"> Programmer </option>
<option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>
现在确定下拉菜单
选择dropdown = new选择(driver.findElement(By.id(names)));
要选择它可以选择'程序员'你可以做
To select its option say 'Programmer' you can do
dropdown.selectByVisibleText(Programmer);
或
dropdown.selectByIndex(1);
或
dropdown.selectByValue("prog");
这篇关于如何使用Java在Selenium WebDriver中选择下拉列表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!