前言:前邊幾篇文章也介紹過鼠標點擊方法
click()
,但是這遠遠無法滿足我們多樣的需求,在 WebDriver 中, 關於鼠標操作的方法封裝在 ActionChains 類中,其中包含右擊、雙擊、拖動、鼠標懸停等等。
一、常用方法
函數名 | 說明 |
---|---|
click(on_element=None) |
點擊鼠標右鍵 |
click_and_hold(on_element=None) |
點擊鼠標左鍵,不鬆開 |
release(on_element=None) |
在某個元素位置鬆開鼠標左鍵 |
context_click(on_element=None) |
點擊鼠標右鍵 |
double_click(on_element=None) |
雙擊鼠標左鍵 |
drag_and_drop(source, target) |
拖拽到某個元素然後鬆開 |
drag_and_drop_by_offset(source, xoffset, yoffset) |
拽到某個座標然後鬆開 |
move_by_offset(xoffset, yoffset) |
鼠標從當前位置移動到某個座標 |
move_to_element(to_element) |
鼠標移動到某個元素 |
move_to_element_with_offset(to_element, xoffset, yoffset) |
移動到距某個元素(左上角座標)多少距離的位置 |
perform() |
執行所有 ActionChains 中存儲的行爲,相當於提交 |
二、代碼示例
選幾個經常使用的測試一下,其他事件語法相同
from selenium import webdriver
import time
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
driver.get("https://www.baidu.cn")
#定位到需要右擊的元素,然後執行鼠標右擊操作(例:對新聞標籤進行右擊)
context_click_location = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/div[3]/a[1]')
ActionChains(driver).context_click(context_click_location).perform()
time.sleep(2) #睡兩秒,看一下效果
# 定位到需要懸停的元素,然後執行鼠標懸停操作(例:對設置標籤進行懸停)
move_to_element_location = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div[3]/a[8]")
ActionChains(driver).move_to_element(move_to_element_location).perform()
time.sleep(2) #睡兩秒,看一下效果
# 鼠標懸浮後點擊高級搜索
driver.find_element_by_xpath("/html/body/div[1]/div[6]/a[2]").click()
time.sleep(2) #睡兩秒,看一下效果
driver.quit() #關閉所有標籤頁
由於百度沒有可拖動的元素,所以在菜鳥上找了一個網址進行測試,由於菜鳥上的網頁是使用frame
內嵌的,所以添加了個處理frame
的過程,關於frame
的處理請參考我的另一篇文章:Python爬蟲 - Selenium(8)frame/iframe表單嵌套頁面
from selenium import webdriver
from selenium.webdriver import ActionChains
import time
driver = webdriver.Chrome()
driver.get("https://www.runoob.com/try/try.php?filename=jqueryui-example-draggable-scroll")
# 切換到目標元素所在的frame
driver.switch_to.frame("iframeResult")
# 確定拖拽目標的起點和終點,完成拖拽
start_location = driver.find_element_by_id("draggable")
end_location = driver.find_element_by_id("draggable3")
ActionChains(driver).drag_and_drop(start_location,end_location).perform()
time.sleep(2) #睡兩秒,看一下效果
driver.quit() #關閉所有標籤頁
Selenium文集傳送門:
標題 | 簡介 |
---|---|
Python爬蟲 - Selenium(1)安裝和簡單使用 | 詳細介紹Selenium的依賴環境在Windows和Centos7上的安裝及簡單使用 |
Python爬蟲 - Selenium(2)元素定位和WebDriver常用方法 | 詳細介紹定位元素的8種方式並配合點擊和輸入、提交、獲取斷言信息等方法的使用 |
Python爬蟲 - Selenium(3)控制瀏覽器的常用方法 | 詳細介紹自定義瀏覽器窗口大小或全屏、控制瀏覽器後退、前進、刷新瀏覽器等方法的使用 |
Python爬蟲 - Selenium(4)配置啓動項參數 | 詳細介紹Selenium啓動項參數的配置,其中包括無界面模式、瀏覽器窗口大小設置、瀏覽器User-Agent (請求頭)等等 |
Python爬蟲 - Selenium(5)鼠標事件 | 詳細介紹鼠標右擊、雙擊、拖動、鼠標懸停等方法的使用 |
Python爬蟲 - Selenium(6)鍵盤事件 | 詳細介紹鍵盤的操作,幾乎包含所有常用按鍵以及組合鍵 |
Python爬蟲 - Selenium(7)多窗口切換 | 詳細介紹Selenium是如何實現在不同的窗口之間自由切換 |
Python爬蟲 - Selenium(8)frame/iframe表單嵌套頁面 | 詳細介紹如何從當前定位的主體切換爲frame/iframe表單的內嵌頁面中 |
Python爬蟲 - Selenium(9)警告框(彈窗)處理 | 詳細介紹如何定位並處理多類警告彈窗 |
Python爬蟲 - Selenium(10)下拉框處理 | 詳細介紹如何靈活的定位並處理下拉框 |
Python爬蟲 - Selenium(11)文件上傳 | 詳細介紹如何優雅的通過send_keys()指定文件進行上傳 |
Python爬蟲 - Selenium(12)獲取登錄Cookies,並添加Cookies自動登錄 | 詳細介紹如何獲取Cookies和使用Cookies進行自動登錄 |
Python爬蟲 - Selenium(13)設置元素等待 | 詳細介紹如何優雅的設置元素等待時間,防止程序運行過快而導致元素定位失敗 |
Python爬蟲 - Selenium(14)窗口截圖 | 詳細介紹如何使用窗口截圖 |
Python爬蟲 - Selenium(15)關閉瀏覽器 | 詳細介紹兩種關閉窗口的區別 |
歡迎留言吐槽