Selenium-Java版(选择框)

选择框

前言

radio框

checkbox框

select框

根据选项的value属性值选择元素 

根据选项的次序(从0开始)选择元素 

根据选项的可见文本选择元素 

去除 

Select单选框 

Select多选框


前言

参考教程: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();
    }
}

智能网联汽车的安全员高级考试涉及多个方面的专业知识,包括但不限于自动驾驶技术原理、车辆传感器融合、网络安全防护以及法律法规等内容。以下是针对该主题的一些核心知识点解析: ### 关于智能网联车安全员高级考试的核心内容 #### 1. 自动驾驶分级标准 国际自动机工程师学会(SAE International)定义了六个级别的自动驾驶等级,从L0到L5[^1]。其中,L3及以上级别需要安全员具备更高的应急处理能力。 #### 2. 车辆感知系统的组成与功能 智能网联车通常配备多种传感器,如激光雷达、毫米波雷达、摄像头超声波传感器等。这些设备协同工作以实现环境感知、障碍物检测等功能[^2]。 #### 3. 数据通信与网络安全 智能网联车依赖V2X(Vehicle-to-Everything)技术进行数据交换,在此过程中需防范潜在的网络攻击风险,例如中间人攻击或恶意软件入侵[^3]。 #### 4. 法律法规要求 不同国家地区对于无人驾驶测试及运营有着严格的规定,考生应熟悉当地交通法典中有关自动化驾驶部分的具体条款[^4]。 ```python # 示例代码:模拟简单决策逻辑 def decide_action(sensor_data): if sensor_data['obstacle'] and not sensor_data['emergency']: return 'slow_down' elif sensor_data['pedestrian_crossing']: return 'stop_and_yield' else: return 'continue_driving' example_input = {'obstacle': True, 'emergency': False, 'pedestrian_crossing': False} action = decide_action(example_input) print(f"Action to take: {action}") ``` 需要注意的是,“橙点同学”作为特定平台上的学习资源名称,并不提供官方认证的标准答案集;建议通过正规渠道获取教材并参加培训课程来准备此类资格认证考试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值