1. 程式人生 > 實用技巧 >Python+selenium 滑鼠三種(雙擊/右擊/懸浮)操作方式(附程式碼!!)

Python+selenium 滑鼠三種(雙擊/右擊/懸浮)操作方式(附程式碼!!)

# #!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2020/7/29 9:29
# @Author : Gengwu
# @FileName: Mouse_Acton.py
# @Software: PyCharm

from selenium import webdriver
from time import sleep #時間類
from selenium.webdriver.common.action_chains import  ActionChains #需要引入ActionChains類,裡面有滑鼠呼叫的方法

driver=webdriver.Chrome() #
開啟chrome瀏覽器 driver.get('https://www.baidu.com/') #開啟百度地址 driver.maximize_window() #視窗最大化 sleep(3) driver.find_element_by_css_selector('#kw').send_keys('python') #定位到搜尋框按鈕,並輸入python #獲取搜尋框元素物件 element=driver.find_element_by_css_selector('#kw') #儲存到變數裡面,定位到搜尋框 sleep(3) #雙擊操作 ActionChains(driver).double_click(element).perform() #
在搜尋框按鈕裡面雙擊,perform執行操作. sleep(2) #右擊操作 ActionChains(driver).context_click(element).perform() #在搜尋框按鈕裡面右擊,perform執行操作. sleep(2) #滑鼠懸停 above=driver.find_element_by_name("tj_settingicon") #通過name找到設定按鈕 ActionChains(driver).move_to_element(above).perform() #move_to_element移到設定的元素,avove上面定位到的設定.然後執行操作
sleep(4) driver.quit()#退出瀏覽器