首页 > 其他 > 详细

selenium--单选框和复选框的操作

时间:2019-08-25 15:11:30      阅读:109      评论:0      收藏:0      [点我收藏+]

单选框操作

from selenium import webdriver
import unittest


class Test_radio(unittest.TestCase):
    def test_SelectRadio(self):
        url = D:\hh.html
        self.driver = webdriver.Chrome()
        self.driver.get(url)
        # 查找所有name属性为’fruit‘的单选框元素对象,并放在列表中
        radioList = self.driver.find_elements_by_xpath(//input[@name="fruit"])
        ‘‘‘
        循环遍历radioList中的每个单选按钮,查找        
        value属性值为’orange‘的单选框
        如果找到后,发现未处于选中状态,则调用click方法选中
        ‘‘‘
        for radio in radioList:
            if radio.get_attribute(value) == orange:
                if not radio.is_selected():
                    radio.click()


test1 = Test_radio()
test1.test_SelectRadio()

复选框的操作

from selenium import webdriver
import unittest


class Test_CheckBox(unittest.TestCase):
    def test_SelectCheckBox(self):
        url = D:\hh.html
        self.driver = webdriver.Chrome()
        self.driver.get(url)
        
        berry = self.driver.find_element_by_xpath(//input[@value="berry"])
        berry.click()

        # 断言草莓复选框被成功选中
        self.assertTrue(berry.is_selected(), 草莓复选框未被选中)

        # 选中则取消
        if berry.is_selected():
            berry.click()
            # 断言未被选中
            self.assertFalse(berry.is_selected())
            # 查找所有name属性为’fruit‘的单选框元素对象,并放在列表中

        CheckBoxList = self.driver.find_elements_by_xpath(//input[@name="fruit"])
        # 遍历CheckBoxList列表中的所有复选框元素,让全部复选框处于被选中状态
        for box in CheckBoxList:
            if not box.is_selected():
                box.click()


test1 = Test_CheckBox()
test1.test_SelectCheckBox()

 

selenium--单选框和复选框的操作

原文:https://www.cnblogs.com/zouzou-busy/p/11219872.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!