selenium webdriver (12) -- 鼠标和键盘
生活随笔
收集整理的這篇文章主要介紹了
selenium webdriver (12) -- 鼠标和键盘
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
在頁面中不是每個元素都是可見的,很多時候需要鼠標操作后才會顯示那個元素,因此selenium提供了ActionChains類專門處理這類操作,這個動作可以是單一的,也可以是組合的,來彌補之前定位元素的不足
ActionChains perform() 執行所有ActionChain的動作 context_click() 右擊 double_click() 雙擊 drag_and_drop() 拖動 move_to_element() 鼠標懸停下面這個例子展示的是在360云盤中,右擊一個文件夾,顯示很多選項,再把鼠標移到移動item,進行點擊的過程
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.action_chains import ActionChains import timedriver = 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() #點擊移動選項 #?
參考:
《Selenium2自動化測試實戰》
《selenium python buildings release 2》
轉載于:https://my.oschina.net/hding/blog/725979
總結
以上是生活随笔為你收集整理的selenium webdriver (12) -- 鼠标和键盘的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mysql学习总结(25)——MySQL
- 下一篇: 2016/7/29作业