且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-02-04 22:15:35

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