Selenium WebDriver-操作復選框


#encoding=utf-8
import unittest
import time
import chardet
from selenium import webdriver
 
class VisitSogouByIE(unittest.TestCase):

    def setUp(self):
        #啟動IE瀏覽器
        #self.driver = webdriver.Firefox(executable_path = "e:\\geckodriver")
        self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")
        
    def test_operateCheckBox(self):
        url = "http://127.0.0.1/test_checkbox.html"
        # 訪問自定義的html網頁
        self.driver.get(url)
        # 使用xpath定位獲取value屬性值為'berry'的input元素對象,也就是“草莓”選項
        berryCheckBox = self.driver.find_element_by_xpath("//input[@value='berry']")
        # 點擊選擇“草莓”選項
        berryCheckBox.click()
        # 斷言“草莓”復選框被成功選中
        self.assertTrue(berryCheckBox.is_selected(), u"草莓復選框未被選中!")
        if berryCheckBox.is_selected():
            # 如果“草莓”復選框被成功選中,再次點擊取消選中
            berryCheckBox.click()
            # 斷言“草莓”復選框處於未選中狀態
            self.assertFalse(berryCheckBox.is_selected())
        # 查找所有name屬性值為“fruit”的復選框元素對象,並存放在checkBoxList列表中
        checkBoxList = self.driver.find_elements_by_xpath("//input[@name='fruit']")
        # 遍歷遍歷checkBoxList列表中的所有復選框元素,讓全部復選框處於被選中狀態
        for box in checkBoxList:
            if not box.is_selected():
                box.click()
        time.sleep(3)



    def tearDown(self):
        # 退出IE瀏覽器
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM