python+selenium2自动化---CheckBox和radiobutton定位
2021-04-09 04:29
标签:init self family login NPU sel action wim dirname 下面直接通过示例代码说明 本地写一个简单的form表单: py测试代码: python+selenium2自动化---CheckBox和radiobutton定位 标签:init self family login NPU sel action wim dirname 原文地址:https://www.cnblogs.com/Xiaojiangzi/p/13375596.html#form1.html
DOCTYPE html>
html lang="en">
head>
meta charset="UTF-8">
title>Titletitle>
head>
body>
form action="javascript:alert(‘test‘)">
swimming:input type="checkbox" name="swimming" value="swimming">br>
reading:input type="checkbox" name="reading" value="reading">br>
br>
gender:label>input type="radio" name="gender" value="Male">男label>
label>input type="radio" name="gender" value="Female">女label>
br>
input type="submit" value="login">
form>
body>
html>from selenium import webdriver
import os
from time import sleep
class TestCase():
def __init__(self):
self.driver = webdriver.Chrome()
html_path = os.path.dirname(os.path.abspath(__file__))
# 本地的html文件地址拼接
file_path = "file:///" + html_path + ‘/form1.html‘
self.driver.get(file_path)
def test_checbox(self):
swimming = self.driver.find_element_by_name(‘swimming‘)
reading = self.driver.find_element_by_name(‘reading‘)
sleep(3)
# 如果没有被选中,则选中
if not swimming.is_selected():
swimming.click()
if not reading.is_selected():
reading.click()
sleep(3)
# 反选操作
swimming.click()
reading.click()
sleep(3)
self.driver.quit()
def test_radio(self):
gender = self.driver.find_elements_by_name(‘gender‘)
# 可以通过属性值来判断勾选哪个值
for el in gender:
if el.get_attribute(‘value‘) == ‘Male‘:
el.click()
print(‘男‘)
sleep(2)
# 也可以直接勾选第二个标签元素
gender[1].click()
sleep(3)
self.driver.quit()
if __name__ == ‘__main__‘:
TestCase().test_radio()
文章标题:python+selenium2自动化---CheckBox和radiobutton定位
文章链接:http://soscw.com/index.php/essay/73186.html