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