选择框
前言
参考教程:Python + Selenium Web自动化 2024版 - 自动化测试 爬虫_哔哩哔哩_bilibili
上期文章:Selenium-Java版(frame切换/窗口切换)-CSDN博客
radio框
<div id="s_radio">
<input type="radio" name="teacher" value="小江老师">小江老师<br>
<input type="radio" name="teacher" value="小雷老师">小雷老师<br>
<input type="radio" name="teacher" value="小凯老师" checked="checked">小凯老师
</div>
radio框选择选项,直接用WebElement的click方法
先打印当前选中的老师名字
再选择小雷老师
运行代码
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.time.Duration;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 创建WebDriver对象
WebDriver wd = new EdgeDriver();
wd.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// 访问目标网页
wd.get("https://www.byhy.net/cdn2/files/selenium/test2.html");
// 获取当前选中的元素
WebElement element = wd.findElement(By.cssSelector("#s_radio input[checked='checked']"));
System.out.println("当前选中的是: " + element.getAttribute("value"));
// 点选 小雷老师
wd.findElement(By.cssSelector("#s_radio input[value='小雷老师']")).click();
// 创建Scanner对象等待用户输入
Scanner scanner = new Scanner(System.in);
System.out.println("等待回车键结束程序");
scanner.next();
// 关闭浏览器
wd.quit();
}
运行框输出小凯老师,代表一开始是小凯老师,自动打开网站选中了小雷老师
checkbox框
<div id="s_checkbox">
<input type="checkbox" name="teacher" value="小江老师">小江老师<br>
<input type="checkbox" name="teacher" value="小雷老师">小雷老师<br>
<input type="checkbox" name="teacher" value="小凯老师" checked="checked">小凯老师
</div>
对checkbox进行选择,也是直接用WebElement的click方法
先把 已经选中的选项全部点击一下,确保都是未选状态
再点击 小雷老师
运行代码
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.time.Duration;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 创建WebDriver对象
WebDriver wd = new EdgeDriver();
wd.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// 访问目标网页
wd.get("https://www.byhy.net/cdn2/files/selenium/test2.html");
// 先把 已经选中的选项全部点击一下
List<WebElement> elements = wd.findElements(By.cssSelector("#s_checkbox input[checked='checked']"));
for (WebElement element : elements) {
element.click();
}
// 再点击 小雷老师
wd.findElement(By.cssSelector("#s_checkbox input[value='小雷老师']")).click();
// 创建Scanner对象等待用户输入
Scanner scanner = new Scanner(System.in);
System.out.println("等待回车键结束程序");
scanner.next();
// 关闭浏览器
wd.quit();
}
}
自动打开网站选中了小雷老师
select框
根据选项的value属性值
选择元素
HTML
<option value="foo">Bar</option>
根据 foo 这个值选择该选项
select.selectByValue("foo")
根据选项的次序
(从0开始)选择元素
选择第2个选项
select.selectByIndex(1)
根据选项的可见文本
选择元素
<option value="foo">Bar</option>
根据Bar这个内容选择该选项
select.selectByVisibleText("Bar")
去除
根据选项的value属性值去除
选中元素
deselectByValue
根据选项的次序去除
选中元素
deselectByIndex
根据选项的可见文本去除
选中元素
deselectByVisibleText
去除
所有选中元素
deselectAll
Select单选框
不需要管原来选的是什么,直接用Select方法选择即可
运行代码
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.support.ui.Select;
import java.time.Duration;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 创建WebDriver对象
WebDriver wd = new EdgeDriver();
wd.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// 访问目标网页
wd.get("https://www.byhy.net/cdn2/files/selenium/test2.html");
// 创建Select对象
WebElement selectElement = wd.findElement(By.cssSelector("#ss_single"));
Select select = new Select(selectElement);
// 通过 Select 对象选中小雷老师
select.selectByVisibleText("小雷老师");
// 创建Scanner对象等待用户输入
Scanner scanner = new Scanner(System.in);
System.out.println("等待回车键结束程序");
scanner.next();
// 关闭浏览器
wd.quit();
}
}
Select多选框
要先去掉原来已经选中的选项
运行代码
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.support.ui.Select;
import java.time.Duration;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 创建WebDriver对象
WebDriver wd = new EdgeDriver();
wd.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// 访问目标网页
wd.get("https://www.byhy.net/cdn2/files/selenium/test2.html");
// 创建Select对象
WebElement selectElement = wd.findElement(By.id("ss_multi"));
Select select = new Select(selectElement);
// 清除所有 已经选中 的选项
select.deselectAll();
// 选择小雷老师 和 小凯老师
select.selectByVisibleText("小雷老师");
select.selectByVisibleText("小凯老师");
// 创建Scanner对象等待用户输入
Scanner scanner = new Scanner(System.in);
System.out.println("等待回车键结束程序");
scanner.next();
// 关闭浏览器
wd.quit();
}
}