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

单选框操作

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

 

转载于:https://www.cnblogs.com/zouzou-busy/p/11219872.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值