微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

界面效果如下

相关脚本代码如下

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())

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐