selenium鼠标操作 ActionChains对象用法

在这里插入图片描述

1.ActionChains基本用法
首先需要了解ActionChains的执行原理,当你调用ActionChains的方法时,不会立即执行,而是会将所有的操作按顺序存放在一个队列里,当你调用perform()方法时,队列中的时间会依次执行。

2、ActionChains方法列表

click_and_hold(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) ——拖拽到某个坐标然后松开

key_down(value, element=None) ——按下某个键盘上的键
key_up(value, element=None) ——松开某个键

move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标
move_to_element(to_element) ——鼠标移动到某个元素
move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置

perform() ——执行链中的所有动作
release(on_element=None) ——在某个元素位置松开鼠标左键
send_keys(*keys_to_send) ——发送某个键到当前焦点的元素
send_keys_to_element(element, *keys_to_send) ——发送某个键到指定元素

一、点击操作
click_and_hold(on_element=None) ——点击鼠标左键,不松开
context_click(on_element=None) ——点击鼠标右键
double_click(on_element=None) ——双击鼠标左键

练习网址 : http://sahitest.com/demo/clicks.htm
实例代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.action_chains import ActionChains
import time

# chrome驱动器 地址
webdriver_file = Service(r'F:\chromedriver.exe')
# 创建webdriver 对象 指明Chrome驱动器地址
wd = webdriver.Chrome(service=webdriver_file)

# 等待时间
wd.implicitly_wait(5)

web_file = r'http://sahitest.com/demo/clicks.htm'
wd.get(web_file)

# 点击CheckBox按钮
wd.find_element(By.CSS_SELECTOR,'''input[οnclick="onclick1('c1')"]''').click()
time.sleep(2)

# 创建一个actionChains类
ac = ActionChains(wd)
# - click_and_hold 点击鼠标左键,不松开
ac.click_and_hold(
    wd.find_element(By.CSS_SELECTOR,'input[name="t1"]')
).perform()

# - context_click 点击鼠标右键
ac.context_click(
    wd.find_element(By.CSS_SELECTOR,'input[value="right click me"]')
).perform()
time.sleep(2)

# - double_click 双击鼠标左键
ac.double_click(
    wd.find_element(By.CSS_SELECTOR,'input[value="dbl click me"]')
).perform()
二、鼠标移动
move_to_element(to_element) ——鼠标移动到某个元素 

move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标

move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置

练习网址 : http://sahitest.com/demo/mouseover.htm

只有方法一:鼠标移动到某个元素的会经常用到,后面两个使用位置关系来移动鼠标,几乎用不到。

一般很少用位置关系来移动鼠标,如果需要,可参考下面的链接来测量元素位置
http://jingyan.baidu.com/article/eb9f7b6d87e2ae869264e847.html

"""
@Project :study 
@Author : 文跃锐(yuerwen)
@Time   : 2021/12/28
@File   :yuerwen_test_02.py
"""
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

chromedriver_file = Service(r'd:\webdrivers\chromedriver.exe')
wd = webdriver.Chrome(service=chromedriver_file)

# 创建等待时间
wd.implicitly_wait(5)

# 打开指定网页地址
web_files = r'http://sahitest.com/demo/mouseover.htm'
wd.get(web_files)

Write_on_hover_element = wd.find_element(By.CSS_SELECTOR,'input[value="Write on hover"]')
Blank_on_hoverelement = wd.find_element(By.CSS_SELECTOR,'input[value="Blank on hover"]')
Kamlesh = wd.find_element(By.CSS_SELECTOR,'a[href="x"]')

# 创建一个actionChains类对象
ac = ActionChains(wd)
t1_element = wd.find_element(By.NAME,'t1')

# -- 链式写法
# ac.move_to_element(Write_on_hover_element).\
# 	move_to_element(Blank_on_hoverelement).move_to_element(Kamlesh).perform()

# -- 分布式写法
# - move_to_element() 鼠标移动到某个元素
ac.move_to_element(
	wd.find_element(By.CSS_SELECTOR,'input[value="Write on hover"]')
).perform()

ac.move_to_element(
	wd.find_element(By.CSS_SELECTOR,'input[value="Blank on hover"]')
).perform()

ac.move_to_element(
	wd.find_element(By.CSS_SELECTOR,'a[href="x"]')
).perform()

# - move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标
# 这种使用位置关系来移动鼠标,几乎用不到
ac.move_by_offset(10, 50).perform()
print(t1_element.get_attribute('value'))

# 关闭浏览器
wd.quit()
三、鼠标拖拽
drag_and_drop(source, target) ——拖拽到某个元素然后松开

drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开

练习网址:http://sahitest.com/demo/dragDropMooTools.htm

"""
@Project :study 
@Author : 文跃锐(yuerwen)
@Time   : 2021/12/28
@File   :yuerwen_test_03.py
"""
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


chromedriver_file = Service(r'd:\webdrivers\chromedriver.exe')
wd = webdriver.Chrome(service=chromedriver_file)

# 创建等待时间
wd.implicitly_wait(5)

# 打开指定网页地址
web_files = r'http://sahitest.com/demo/dragDropMooTools.htm'
wd.get(web_files)

# 被拖拽的元素
source_dragger = wd.find_element(By.CSS_SELECTOR,'#dragger')
target_items = wd.find_elements(By.CSS_SELECTOR,'body .item')

# 创建一个actionChains类对象
ac = ActionChains(wd)
#  drag_and_drop(source, target) ——拖拽到某个元素然后松开
ac.drag_and_drop(source_dragger,target_items[1]).perform()

# 关闭浏览器
wd.quit()

drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开
一般用坐标定位很少,用上例中的方法1足够了,如果看源码,会发现方法2其实就是方法1中的drag_and_drop()的实现。注意:拖拽使用时注意加等待时间,有时会因为速度太快而失败。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuerwen_python

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值