码迷,mamicode.com
首页 > 编程语言 > 详细

python+selenium2自动化---CheckBox和radiobutton定位

时间:2020-07-25 23:33:17      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:init   self   family   login   NPU   sel   action   wim   dirname   

下面直接通过示例代码说明

本地写一个简单的form表单:

#form1.html
<!
DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </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>

py测试代码:

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定位

标签:init   self   family   login   NPU   sel   action   wim   dirname   

原文地址:https://www.cnblogs.com/Xiaojiangzi/p/13375596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!