java【selenium】操作多选元素框

本文介绍使用Selenium WebDriver进行单选框、复选框及下拉列表等Web控件的操作方法,包括如何选择和取消选择选项,并展示了通过不同方式(如值、索引和文本)来操作控件的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import static org.junit.jupiter.api.Assertions.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

class RadioButtonsAndcheckboxes {
/*
* 单选框和复选框操作
*/
//引入Webdriver
WebDriver driver;
//访问网址
String baseurl;

@BeforeEach
void setUp() throws Exception {
	//谷歌浏览器本地驱动
	System.setProperty("webdriver.chrome.driver", "/Users/lisen/webselenium/selenium/chromedriver");
	//初始化谷歌浏览器
	driver=new ChromeDriver();
	baseurl="file:///Users/lisen/Downloads/PracticePage.html";
	//设置隐性等待
	driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	//窗口最大化
	driver.manage().window().maximize();
	
	

}

@Test
void test() throws Exception {
	//访问网址
	driver.get(baseurl);
	//打印log
	System.out.println("打开网址完成");
	//查找多选元素列表框
	WebElement element =driver.findElement(By.id("multiple-select-example"));
	//查找Select属性的控件
	Select sel = new Select(element);
	//等待3秒
	Thread.sleep(3000);
	//用Value选中控件元素
	sel.selectByValue("orange");
	//用Value取消选中元素
	sel.deselectByValue("orange");
	//用角标选中控件元素
	sel.selectByIndex(2);
	//用角标取消选中控件元素
	sel.deselectByIndex(2);getClass();
	//用文本选中控件元素
	sel.selectByVisibleText("苹果");
	//用文本取消选中控件元素
	sel.deselectByVisibleText("苹果");
	//打印所有选中的选项
	List<WebElement> selectopions=sel.getAllSelectedOptions();
	for (WebElement option:selectopions) {
		//文本返回所有的选中元素
		System.out.println("打印所有元素"+option.getText());
		
	}
	
	//等待2秒钟
	Thread.sleep(2000);
	//取消所有选中按钮
	sel.deselectAll();
	
	
	
	
}

@AfterEach
void tearDown() throws Exception {
	//等待3秒
	Thread.sleep(3000);
	//关闭浏览器
	driver.quit();

}

}

### Java Selenium 中对 `<select>` 元素赋值的方法 在 SeleniumJava API 中,`<select>` 元素操作可以通过 `Select` 类来实现。该类提供了种方法用于选择项,例如通过索引、可见文本或值属性进行选择。 以下是具体实现的方式: #### 1. 导入必要的依赖项 为了使用 Selenium 和其功能模块,在项目的 Maven 配置文件中需引入以下依赖项[^3]: ```xml <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.8.0</version> </dependency> ``` #### 2. 初始化 WebDriver 并加载目标网页 初始化 WebDriver 实例并导航到包含 `<select>` 元素的目标页面: ```java WebDriver driver = new ChromeDriver(); driver.get("http://example.com"); ``` #### 3. 定位 `<select>` 元素 可以使用不同的定位策略找到 `<select>` 元素。常见的有 ID、名称、CSS Selector 或 XPath 等方式[^1]: ```java WebElement selectElement = driver.findElement(By.id("dropdown")); // 或者使用其他定位方式 // WebElement selectElement = driver.findElement(By.name("dropdownName")); // WebElement selectElement = driver.findElement(By.cssSelector("#dropdown")); // WebElement selectElement = driver.findElement(By.xpath("//select[@id='dropdown']")); ``` #### 4. 创建 Select 对象实例 一旦找到了 `<select>` 元素,就可以创建一个 `Select` 对象来进行进一步操作: ```java import org.openqa.selenium.support.ui.Select; Select dropdown = new Select(selectElement); ``` #### 5. 赋值给 `<select>` 元素 `Select` 类提供三种主要的方式来设置中的项: - **按索引选择**: 使用 `selectByIndex(int index)` 方法。 - **按可见文本选择**: 使用 `selectByVisibleText(String text)` 方法。 - **按值属性选择**: 使用 `selectByValue(String value)` 方法。 下面是一个完整的例子展示这三种方式的用法: ```java // 按索引选择 (假设第一个项是默认值) dropdown.selectByIndex(1); // 按可见文本选择 dropdown.selectByVisibleText("Option Text"); // 按值属性选择 dropdown.selectByValue("optionValue"); ``` 以上代码片段展示了如何针对不同需求选择合适的项。 #### 注意事项 如果 `<select>` 是一个多选框,则需要调用 `isMultiple()` 来确认是否支持,并可使用 `deselectAll()` 清除所有已项目后再重新选择项。 --- ### 示例完整代码 以下是一份综合示例代码,演示了如何在一个简单的 HTML 页面上完成上述过程: ```html <!-- Example.html --> <select id="dropdown"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="mercedes">Mercedes</option> <option value="audi">Audi</option> </select> ``` 对应的 Java 测试脚本如下所示: ```java import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.Select; public class DropdownExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("file:///path/to/Example.html"); WebElement dropdownElement = driver.findElement(By.id("dropdown")); Select dropdown = new Select(dropdownElement); // By Index dropdown.selectByIndex(1); // By Visible Text dropdown.selectByVisibleText("Mercedes"); // By Value Attribute dropdown.selectByValue("audi"); } finally { driver.quit(); } } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值