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


思路:

  • 需要引入ActionChains
  • 然后定位相關元素
  • ActionChains().調用相關鼠標操作方法

具體代碼如下:

# #!/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()#退出瀏覽器

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM