zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    前端--HTML
    并发函数--线程
    并发编程--进程
    一个好用的网站,各种在线
    django Models与数据库关系
    流文件下载
    小白都能秒懂的各数据库在Django的配置
    关于django 内建缓存 信号 及自定义json的配置
    django批量创建数据
    关于drf的组件
  • 原文地址:https://www.cnblogs.com/zouzou-busy/p/11219872.html
Copyright © 2011-2022 走看看