1. 介绍
本文通过一个简单示例,演示如何使用Java的Selenium WebDriver操作HTML下拉框。我们将结合JUnit和Selenium,打开https://www.baeldung.com/contact页面,并在"问题类型"下拉框中选择"Bug Reporting"选项。
2. 依赖配置
首先在pom.xml中添加核心依赖:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.18.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version> <scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.7.0</version>
</dependency>
✅ 关键依赖说明:
selenium-java
:Selenium核心库junit-jupiter-engine
:JUnit 5测试引擎webdrivermanager
:自动管理浏览器驱动(避免手动下载驱动的坑)
3. 环境配置
使用Chrome浏览器进行测试,配置如下:
@BeforeEach
public void setUp() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
⚠️ 配置要点:
- 通过
@BeforeEach
注解确保每个测试前初始化环境 - 使用WebDriverManager自动管理驱动,比硬编码路径更灵活
- 需提前在测试机安装Chrome浏览器
测试结束后必须关闭浏览器,避免资源泄漏:
@AfterEach
public void cleanUp() {
driver.close();
}
4. 定位下拉框元素
通过XPath精确定位目标元素:
private static final String URL = "https://www.baeldung.com/contact";
private static final String INPUT_XPATH = "//select[@name='question-recipient']";
private static final String OPTION_XPATH =
"//select[@name='question-recipient']/option[@value='Bug reporting']";
定位策略说明:
- 使用XPath因其强大的定位能力
INPUT_XPATH
:定位下拉框本身(通过name属性)OPTION_XPATH
:精确定位"Bug Reporting"选项(通过value属性)
验证元素存在的测试用例:
@Test
public void givenBaeldungContactPage_whenSelectQuestion_thenContainsOptionBugs() {
driver.get(URL);
WebElement inputElement = driver.findElement(By.xpath(OPTION_XPATH));
assertEquals("Bug reporting", inputElement.getText());
}
5. 操作下拉框选项
Selenium提供了专门的Select
类(org.openqa.selenium.support.ui.Select
)处理下拉框。以下是三种常用操作方式:
5.1 通过value值选择
@Test
public void givenBaeldungContactPage_whenSelectQuestion_thenSelectOptionBugsByValue() {
driver.get(URL);
WebElement inputElement = driver.findElement(By.xpath(INPUT_XPATH));
WebElement optionBug = driver.findElement(By.xpath(OPTION_XPATH));
Select select = new Select(inputElement);
select.selectByValue(OPTION_TEXT);
assertTrue(optionBug.isSelected());
}
5.2 通过可见文本选择
@Test
public void givenBaeldungContactPage_whenSelectQuestion_thenSelectOptionBugsByText() {
driver.get(URL);
WebElement inputElement = driver.findElement(By.xpath(INPUT_XPATH));
WebElement optionBug = driver.findElement(By.xpath(OPTION_XPATH));
select.selectByVisibleText(OPTION_TEXT);
assertTrue(optionBug.isSelected());
}
5.3 通过索引选择
@Test
public void givenBaeldungContactPage_whenSelectQuestion_thenSelectOptionBugsByIndex() {
driver.get(URL);
WebElement inputElement = driver.findElement(By.xpath(INPUT_XPATH));
WebElement optionBug = driver.findElement(By.xpath(OPTION_XPATH));
select.selectByIndex(OPTION_INDEX);
assertTrue(optionBug.isSelected());
}
操作要点对比:
| 方法 | 适用场景 | 注意事项 |
|------|----------|----------|
| selectByValue()
| 知道option的value属性 | ✅ 最稳定 |
| selectByVisibleText()
| 知道页面显示文本 | ❌ 受国际化影响 |
| selectByIndex()
| 知道选项顺序 | ⚠️ 页面结构变更时易失效 |
6. 总结
本文演示了使用Selenium操作下拉框的核心技巧:
标准流程:
- 定位
<select>
元素 - 创建
Select
对象 - 调用
selectByXxx()
方法
- 定位
三种选择方式:
selectByValue()
:推荐首选selectByVisibleText()
:适合动态文本selectByIndex()
:简单粗暴但脆弱
最佳实践:
- 使用WebDriverManager管理驱动
- 优先用value属性定位
- 测试后务必清理资源
完整代码示例请查阅GitHub仓库