selenium webdriver (12) -- 鼠標和鍵盤

在頁面中不是每一個元素都是可見的,不少時候須要鼠標操做後纔會顯示那個元素,所以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 2ui

相關文章
相關標籤/搜索