• 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()
  • 相关阅读:
    *setTimeout实现text输入在用户停顿时才调用事件!*
    *关于TextArea里中英文混合时换行的问题*
    *用C#创建Windows服务(Windows Services)
    *使用C# 创建邮件发送组件(SMTP)
    *ASP.NET 2.0 连接SQL Server数据库详解
    *SQL2000系统表的应用
    *在.NET(正式版)环境下发送邮件
    *如何解决select等元素突出来的问题
    *介绍JSON*
    Tomcat性能配置
  • 原文地址:https://www.cnblogs.com/zouzou-busy/p/11219872.html
Copyright © 2020-2023  润新知