当前位置:   article > 正文

软件测试|使用selenium处理单选框和多选框_selenium 单选按钮

selenium 单选按钮

在这里插入图片描述

简介

我们在web自动化测试工作中,经常会遇到对单选框(Radio Buttons)或者多选框(Checkboxes)进行操作的场景,单选框和多选框主要是用于我们做出选择或提交数据。本文将主要介绍selenium对于单选框和多选框的操作。

处理单选框

单选框允许用户从多个选项中选择一个选项。使用Selenium,我们可以模拟用户在单选框上的选择。页面代码如下:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
</head>
<body>
<fieldset>
<legend>单选按钮radio</legend>
<form action="">
    <label for=""><input type="radio" name="car" value="volkswagen" id="passat">帕萨特</label>
    <label for=""><input type="radio" name="car" value="toyota" id="camry">凯美瑞</label>
    <label for=""><input type="radio" name="car" value="benz" id="c200">奔驰C级</label>
    <label for=""><input type="radio" name="car" value="bmw" id="325i" checked="">宝马3系</label>
    <label for=""><input type="radio" name="car" value="honda" id="accord" disabled="" >雅阁</label>
</form>
</fieldset>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

下面是我们处理单选框的示例,代码如下:

from selenium import webdriver
from selenium.webdriver.common.by import By

# 设置WebDriver路径
driver = webdriver.Chrome()

# 打开一个示例网页
driver.get("https://example.com/radio-buttons")

# 找到单选框元素
radio_button = driver.find_element(By.ID, "option2")

# 检查单选框是否被选中,如果没有选中,则选择它
if not radio_button.is_selected():
    radio_button.click()

# 关闭浏览器
driver.quit()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

处理多选框

多选框即表示我们可以从多个选项中选择多个选项。通过selenium,我们可以模拟用户对多选框的选择操作。示例多选框页面代码如下:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
</head>
<body>
<br>
<fieldset>
<legend>多选按钮checkbox</legend>
<form action="">
    <input type="checkbox" name="checkbox" value="汽车" id="qc">汽车<br>
    <input type="checkbox" name="checkbox" value="购物" id="gw">购物<br>
    <input type="checkbox" name="checkbox" value="旅游" id="ly" readonly="">旅游 <br>
    <input type="checkbox" name="checkbox" value="音乐" id="yy" disabled="">音乐 <br>
</form>
</fieldset>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

要处理这个多选框,我们的示例代码如下:

# 1.导入selenium
from selenium import webdriver
from time import sleep
import os
from selenium.webdriver.common.by import By

# 2.打开浏览器
driver = webdriver.Chrome()

# 3.打开页面
url = "file:///" + os.path.abspath("./1.html")
driver.get(url)
sleep(2)

# 4. 选择部分多选框
# 建立列表填写将要选择的复选框名称
box_list = ["购物", "旅游"]

# 定位所有的复选框
checkboxes = driver.find_elements(By.NAME, "checkbox")

# 遍历选择
for checkbox in checkboxes:
    # 判断获取到的复选框的名称和在需求勾选的复选框中
    if checkbox.get_attribute("value") in box_list:
        # 如果在,判断选框是否被选中
        if checkbox.is_selected():
            pass
        else:
            # 单击一下,让单选按钮进入选中状态
            checkbox.click()
            sleep(1)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

总结

本文主要介绍了selenium对于单选框和多选框的处理,希望本文能够帮助到大家!

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号