python自动化测试selenium操作checkbox和radiobox技术

这篇文章主要为大家介绍了python自动化测试selenium核心技术操作checkbox和radiobox的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助
form表单中经常涉及复选框(checkbox)和单选框(radiobox),如用户的爱好跑步、游泳、跳舞可以使用复选框,性别男、女可以使用单选框。
(1)checkbox选择或反选:使用click()方法
(2)radiobox有相同的名称,多个值,可先通过名称获得,再通过值判断,选择使用click()方法。
示例页面:
页面代码:
Title
测试from表单操作checkbox和radiobutton
跑步:
游泳:
跳舞:
<!--
分界线-->
性别:
男:
女:
示例脚本:
import os
from selenium import webdriver
from time import sleep
class TestCheckBoxOrRadioBtn(object):
def setup(self):
self.driver = webdriver.Chrome()
path = os.path.dirname(os.path.abspath(__file__))
file_path = 'file:///'+path+'/html/form.html'
self.driver.get(file_path)
def test_checkbox(self):
#定位跳舞
dancing=self.driver.find_element_by_name("dancing")
#如果没有选择,则点击选择
if not dancing.is_selected():
dancing.click()
sleep(2)
running = self.driver.find_element_by_name("running")
if not running.is_selected():
running.click()
sleep(2)
swimming = self.driver.find_element_by_name("swimming")
if not swimming.is_selected():
swimming.click()
sleep(2)
#再次点击取消选择游泳
swimming.click()
sleep(2)
self.driver.quit()
def test_radio(self):
#获得元素列表
gender= self.driver.find_elements_by_name("gender")
#选中性别男
gender[0].click()
sleep(2)
# 选中性别女
gender[1].click()
sleep(2)
self.driver.quit()
if __name__ == '__main__':
case = TestCheckBoxOrRadioBtn()
case.test_checkbox()
case.test_radio()
运行结果:
以上就是python自动化测试selenium操作checkbox和radiobox技术的详细内容,更多关于selenium操作checkbox和radiobox的资料请关注昆居客【www.kunjuke.com】其它相关文章!