#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/1/11 11:19 # @File : unittest_test9_2.py ''' 鼠标事件:鼠标移动 ''' from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions from selenium.webdriver.common.action_chains import ActionChains import unittest import time class ToolTipTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.implicitly_wait(30) self.driver.get("http://jqueryui.com/tooltip/") def test_tool_tip(self): frame_ele = self.driver.find_element_by_class_name('demo-frame') self.driver.switch_to.frame(frame_ele) #定位搜索框age age_filed = self.driver.find_element_by_id('age') #移动鼠标至搜索框,注意,鼠标移动后必须执行提交perform()功能 ActionChains(self.driver).move_to_element(age_filed).perform() time.sleep(2) #等待 tool_tip_ele = WebDriverWait(self.driver,10) .until(expected_conditions.visibility_of_element_located((By.CLASS_NAME,'ui-helper-hidden-accessible'))) #print(tool_tip_ele.text) # 鼠标右键 ActionChains(self.driver).context_click(age_filed) time.sleep(2) #判断 self.assertEqual('We ask for your age only for statistical purposes.',tool_tip_ele.text) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main(verbosity=2)
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/1/11 15:37 # @File : unittest_test9_3.py ''' 鼠标事件:双击操作 ''' from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import unittest import time class DoubleClickTest(unittest.TestCase): URL = 'http://api.jquery.com/dblclick/' def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.implicitly_wait(30) self.driver.get(self.URL) def test_tool_tip(self): frame = self.driver.find_element_by_tag_name('iframe') self.driver.switch_to.frame(frame) box = self.driver.find_element_by_tag_name('div') #验证box中颜色是blue, # rgba(0,0,255,1),rgba括号中前3个数字代表着 red green blue三种颜色的rgb值,0-255, # 最后一个是设定这个颜色的透明度即alpha值。范围从0到1,越接近1,代表透明度越低 # value_of_css_property(),css属性值 self.assertEqual("rgba(0, 0, 255, 1)",box.value_of_css_property('background-color')) #移动鼠标至span ActionChains(self.driver).move_to_element(self.driver.find_element_by_tag_name('span')).perform() #鼠标双击box ActionChains(self.driver).double_click(box).perform() time.sleep(2) #验证box中颜色是yellow self.assertEqual("rgba(255, 255, 0, 1)",box.value_of_css_property('background-color')) def tearDown(self): self.driver.close() if __name__ == "__main__": unittest.main(verbosity=2)
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/1/11 16:00 # @File : unittest_test9_4.py ''' 鼠标事件:drag_and_drop()方法时间鼠标拖放 ''' from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import unittest import time class DragAndDropTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.implicitly_wait(30) self.driver.get('http://jqueryui.com/resources/demos/droppable/default.html') def test_drag_and_drop(self): source = self.driver.find_element_by_id('draggable') target = self.driver.find_element_by_id('droppable') #把source拖至target ActionChains(self.driver).drag_and_drop(source,target).perform() #验证拖放成功 self.assertEqual('Dropped!',target.text) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main(verbosity=2)