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()
  • 相关阅读:
    Codeforces Round #592 (Div. 2)C. The Football Season(暴力,循环节)
    Educational Codeforces Round 72 (Rated for Div. 2)D. Coloring Edges(想法)
    扩展KMP
    poj 1699 Best Sequence(dfs)
    KMP(思路分析)
    poj 1950 Dessert(dfs)
    poj 3278 Catch That Cow(BFS)
    素数环(回溯)
    sort与qsort
    poj 1952 buy low buy lower(DP)
  • 原文地址:https://www.cnblogs.com/zouzou-busy/p/11219872.html
Copyright © 2011-2022 走看看