Selenium_单选框和复选框的选中状态判定以及元素是否可用和可见判定(10)

 简单写个单选框和复选框界面

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>test</title>
    
</head>

<body bgcolor="burlywood">
    <form>
        <input type="radio" name="sex" value="male">Male<br>
        <input type="radio" name="sex" value="female">Female<br>
        <input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
    </form>
    
</body>

</html>

界面效果如下

Selenium_单选框和复选框的选中状态判定以及元素是否可用和可见判定(10)

相关脚本代码如下

import time
from selenium import webdriver
from selenium.webdriver import ActionChains

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("file:///D:/Users/User/Desktop/test.html")

# 检查单选框是否被选择
radios = driver.find_elements_by_xpath("//*[@type='radio']")
radio1 = radios[0]
radio2 = radios[1]
radio1.click()
# 检查单选框是否被选中,选中返回True
print(radio1.is_selected())
radio2.click()
print(radio1.is_selected())
time.sleep(2)

# 检查复选框是否被勾选
checkbox = driver.find_element_by_xpath("//*[@type='checkbox']")
checkbox.click()
print(checkbox.is_selected())
time.sleep(2)
checkbox.click()
print(checkbox.is_selected())

# 检查元素是否可用,可用返回True
print(checkbox.is_enabled())

# 检查元素是否显示,显示返回True
print(checkbox.is_displayed())

 

上一篇:ListItem布局中的checkbox阻碍整行选择的处理办法


下一篇:复选框样式修改