selenium提供鼠标操作的方法及步骤: ·需要导入ActionChains类 · 通过ActionChains实例化鼠标对象 action = ActionChains(driver) # driver表示的是浏览器驱动对象 · 调用鼠标的事件方法 · 调用鼠标的执行方法 action.perform() 1.2 clickAndHold单击(不释放)
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Chrome(executable_path='./chromedriver') as driver:
# 打开本地文件中的html文件
driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')
# click_and_hold 点击且不松开
div = driver.find_element_by_xpath('//div[@onmousedown="mDown(this)"]')
webdriver.ActionChains(driver).click_and_hold(div).perform()
time.sleep(2)
1.3context_click单击
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
dr = webdriver.Chrome()
url = r'http://www.baidu.com'
dr.get(url)
ipt = dr.find_element_by_id('kw')
action = ActionChains(dr)
action.context_click(ipt) # 在输入框中右键操作
action.perform()
time.sleep(3)
dr.quit()
1.4 double_click双击
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Chrome(executable_path='./chromedriver') as driver:
# 打开本地文件中的html文件
driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')
1.5 drag_and_drop拖动
此方法首先在源元素上单击并按住,然后移动到目标元素的位置后释放鼠标.
调用鼠标拖动事件方法 action.drag_and_drop(source, target) # source表示的是源元素,被拖动的元素, target表示是目标源,也就是要拖动到哪个元素上。
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Chrome(executable_path='./chromedriver') as driver:
# 打开本地文件中的html文件
driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')
1.6 move_to_element悬停
此方法将鼠标移到元素的中间. 执行此操作时, 该元素也会滚动到视图中(悬停)
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Chrome(executable_path='./chromedriver') as driver:
# 打开本地文件中的html文件
driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')
1.7 drag_and_drop_by_offset单元素拖动
action.drag_and_drop_by_offset(element, x, y) x, y 表示的元素拖动时横向和纵向移动的距离,单位为像素, element表示的是元素对象 移动的像素最终要比在[url=]web[/url]页面中看到的移动像素值要大,最好大于5个像素或者10像素
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
dr = webdriver.Chrome()
url =
r'file:///C:/Users/tang/Desktop/pagetest/%E9%AA%8C%E8%AF%81%E7%A0%81/index.html'
dr.get(url)
h = dr.find_element_by_css_selector('.handler')
action = ActionChains(dr)
action.drag_and_drop_by_offset(h, 260, 0) # 单元素拖拽 滑动验证
action.perform()
time.sleep(5)
dr.quit()
1.8 moveByOffset:
此方法将鼠标从其当前位置(或0,0)移动给定的偏移量. 如果坐标在视图窗口之外, 则鼠标最终将在浏览器窗口之外.
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Chrome(executable_path='./chromedriver') as driver:
# 打开本地文件中的html文件
driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')
1.9 release(释放)
此操作将释放按下的鼠标左键. 如果WebElement转移了, 它将释放给定WebElement上按下的鼠标左键
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Chrome(executable_path='./chromedriver') as driver:
# 打开本地文件中的html文件
driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')
1.10鼠标链式操作
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
with webdriver.Chrome() as driver:
driver.get('file:///Users/superfan/工作/myproject/study/po/action.html')