Python爬蟲之Selenium鼠標事件的實現(xiàn)
一、常用方法
函數(shù)名 | 說明 |
---|---|
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() | 執(zhí)行所有 ActionChains 中存儲的行為,相當于提交 |
二、代碼示例
選幾個經(jīng)常使用的測試一下,其他事件語法相同
from selenium import webdriver import time from selenium.webdriver import ActionChains driver = webdriver.Chrome() driver.get("https://www.baidu.cn") #定位到需要右擊的元素,然后執(zhí)行鼠標右擊操作(例:對新聞標簽進行右擊) 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) #睡兩秒,看一下效果 # 定位到需要懸停的元素,然后執(zhí)行鼠標懸停操作(例:對設(shè)置標簽進行懸停) 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() #關(guān)閉所有標簽頁
由于百度沒有可拖動的元素,所以在菜鳥上找了一個網(wǎng)址進行測試,由于菜鳥上的網(wǎng)頁是使用frame內(nèi)嵌的,所以添加了個處理frame的過程,關(guān)于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() #關(guān)閉所有標簽頁
Selenium文集傳送門:
標題 | 簡介 |
---|---|
Python爬蟲 - Selenium(1)安裝和簡單使用 | 詳細介紹Selenium的依賴環(huán)境在Windows和Centos7上的安裝及簡單使用 |
Python爬蟲 - Selenium(2)元素定位和WebDriver常用方法 | 詳細介紹定位元素的8種方式并配合點擊和輸入、提交、獲取斷言信息等方法的使用 |
Python爬蟲 - Selenium(3)控制瀏覽器的常用方法 | 詳細介紹自定義瀏覽器窗口大小或全屏、控制瀏覽器后退、前進、刷新瀏覽器等方法的使用 |
Python爬蟲 - Selenium(4)配置啟動項參數(shù) | 詳細介紹Selenium啟動項參數(shù)的配置,其中包括無界面模式、瀏覽器窗口大小設(shè)置、瀏覽器User-Agent (請求頭)等等 |
Python爬蟲 - Selenium(5)鼠標事件 | 詳細介紹鼠標右擊、雙擊、拖動、鼠標懸停等方法的使用 |
Python爬蟲 - Selenium(6)鍵盤事件 | 詳細介紹鍵盤的操作,幾乎包含所有常用按鍵以及組合鍵 |
Python爬蟲 - Selenium(7)多窗口切換 | 詳細介紹Selenium是如何實現(xiàn)在不同的窗口之間自由切換 |
Python爬蟲 - Selenium(8)frame/iframe表單嵌套頁面 | 詳細介紹如何從當前定位的主體切換為frame/iframe表單的內(nèi)嵌頁面中 |
Python爬蟲 - Selenium(9)警告框(彈窗)處理 | 詳細介紹如何定位并處理多類警告彈窗 |
Python爬蟲 - Selenium(10)下拉框處理 | 詳細介紹如何靈活的定位并處理下拉框 |
Python爬蟲 - Selenium(11)文件上傳 | 詳細介紹如何優(yōu)雅的通過send_keys()指定文件進行上傳 |
Python爬蟲 - Selenium(12)獲取登錄Cookies,并添加Cookies自動登錄 | 詳細介紹如何獲取Cookies和使用Cookies進行自動登錄 |
Python爬蟲 - Selenium(13)設(shè)置元素等待 | 詳細介紹如何優(yōu)雅的設(shè)置元素等待時間,防止程序運行過快而導致元素定位失敗 |
Python爬蟲 - Selenium(14)窗口截圖 | 詳細介紹如何使用窗口截圖 |
Python爬蟲 - Selenium(15)關(guān)閉瀏覽器 | 詳細介紹兩種關(guān)閉窗口的區(qū)別 |
到此這篇關(guān)于Python爬蟲之Selenium鼠標事件的實現(xiàn)的文章就介紹到這了,更多相關(guān)Selenium 鼠標事件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

Pandas處理DataFrame稀疏數(shù)據(jù)及維度不匹配數(shù)據(jù)分析詳解

Python高階函數(shù)與裝飾器函數(shù)的深入講解

python django 原生sql 獲取數(shù)據(jù)的例子