在页面中不是每一个元素都是可见的,不少时候须要鼠标操做后才会显示那个元素,所以selenium提供了ActionChains类专门处理这类操做,这个动做能够是单一的,也能够是组合的,来弥补以前定位元素的不足html
ActionChains perform() 执行全部ActionChain的动做 context_click() 右击 double_click() 双击 drag_and_drop() 拖动 move_to_element() 鼠标悬停
下面这个例子展现的是在360云盘中,右击一个文件夹,显示不少选项,再把鼠标移到移动item,进行点击的过程python
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.action_chains import ActionChains import time driver = webdriver.Firefox() driver.get('http://yunpan.360.cn') driver.implicitly_wait(10) driver.find_element_by_name("account").send_keys('XXX') driver.find_element_by_name("password").send_keys('XXX') driver.find_element_by_name("password").submit() time.sleep(10) #right click by mouse right_click = driver.find_element_by_xpath("/html/body/div[3]/div[4]/div[1]/div[6]/div[2]/div/ul/li[1]") #"视频"文件夹 move = driver.find_element_by_xpath("//div[7]/ul/li[6]/a/span") #右击视频文件夹后的移动选项 ActionChains(driver).context_click(right_click).perform() driver.get_screenshot_as_file("c:\\work\\360.png") #move mouse to motivation item ActionChains(driver).move_to_element(move).perform() time.sleep(5) driver.get_screenshot_as_file("c:\\work\\move.png") #click motivation item to move the file[] ActionChains(driver).double_click(move).perform() #点击移动选项 #
参考:web
《Selenium2自动化测试实战》测试
《selenium python buildings release 2》ui