![](/img/trans.png)
[英]How to select and get dropdown value in Selenium WebDriver using Java
[英]How to select a dropdown value in Selenium WebDriver using Java
我是 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>
请给我一些点击下拉菜单的方法。
我尝试使用上面的示例行,但出现错误,例如 Element 当前不可见,因此可能无法与命令持续时间或超时交互:32 毫秒下拉值是 jquery 多选小部件格式。
只需将您的 WebElement 包装到 Select Object 中,如下所示
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>
现在识别下拉菜单
Select dropdown = new Select(driver.findElement(By.id("designation")));
要选择其选项,请说“程序员”,您可以
dropdown.selectByVisibleText("Programmer ");
或者
dropdown.selectByIndex(1);
或者
dropdown.selectByValue("prog");
如果您想在一行中写下所有内容,请尝试
new Select (driver.findElement(By.id("designation"))).selectByVisibleText("Programmer ");
如上所述,我们需要在 Selenium 中实现 Select Class,并且我们可以进一步使用各种可用的方法,例如:-
实际上select
确实选择但不将选定的值放置到相应的字段中。 哪里想知道下面的代码段完美无缺
driver.findElement(By.name("period")).sendKeys("Last 52 Weeks");
使用xpath选择下拉列表的代码
Select select = new
Select(driver.findElement(By.xpath("//select[@id='periodId']));
使用 selectByVisibleText选择特定选项的代码
select.selectByVisibleText(Last 52 Weeks);
您可以使用以下方法来处理 selenium 中的下拉列表。
1. driver.selectByVisibleText("Text");
2. driver.selectByIndex(1);
3. driver.selectByValue("prog");
有关更多详细信息,您可以参考http://www.codealumni.com/handle-drop-selenium-webdriver/这篇文章。
它肯定会对您解决查询有很大帮助。
WebDriver driver = new FirefoxDriver();
WebElement identifier = driver.findElement(By.id("periodId"));
Select select = new Select(identifier);
select.selectByVisibleText("Last 52 Weeks");
我还没有在 Selenium 中尝试过,但是对于 Galen 测试来说这是有效的,
var list = driver.findElementByID("periodID"); // 这将返回 web 元素
列表。点击(); // 这将打开下拉列表。
list.typeText("14w"); // 这将选择选项“14w”。
你可以在 selenium 中试试这个,galen 和 selenium 的工作原理是相似的。
首先将包导入为:
导入 org.openqa.selenium.support.ui.Select;
然后单行写成:
new Select (driver.findElement(By.id("sampleid"))).selectByValue("SampleValue");
尝试这个-
driver.findElement(By.name("period")).sendKeys("Last 52 Weeks");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.