自动化学习【4】- ActionChains基础操作

一、引入依赖包:

from selenium.webdriver.common.action_chains import ActionChains as Action
前言:获取 ActionChains()类的实例:
action = Action(driver)

二、ActionChains 基础操作:

1. 所有 ActionChains 的事件操作最后都需要执行 perform() 才会生效
# 例如执行一个点击操作,也必须执行 perform 方法才可以执行
action.click(element).perform()
2. 鼠标左键点击:click(to_element)
action.click(element).perform()
3. 鼠标右键点击:context_click(to_element)
action.context_click(element).perform()
4. 鼠标左键双击:double_click(to_element)
action.double_click(element).perform()
5. 鼠标左键长按:click_and_hold(to_element)
action.click_and_hold(element).perform()
6. 鼠标移动到页面元素之上:move_to_element(to_element)
action.move_to_element(element).perform()
7. 鼠标移动到页面坐标位置: move_by_offset(xoffset, yoffset)
action.move_by_offset(5,10).perform()	
8. 鼠标从某个元素位置开始移动鼠标坐标:move_to_element_with_offset(to_element, xoffset, yoffset)
action.move_to_element_with_offset(element, 5 , 10).perform()
9. 释放元素上的鼠标控制:release(on_element)
action.release(element).perform()
10、指定时间内暂停所有操作:pause() 单位:秒
action.pause(5).perform()	# 中断操作 5 秒
11 、拖动起始元素到目标元素之上:action.drag_and_drop( source, target )
# 获取 element
source = driver.find_element_by_name('source')
target = driver.find_element_by_name('target')

# 执行拖动操作
action.drag_and_drop(source, target).perform()
12、拖动元素到对应坐标之上: drag_and_drop_by_offset( source, xoffset, yoffset )
# 获取 element
source = driver.find_element_by_name('source')

# 执行拖动操作
action.drag_and_drop_by_offset(source , 5 ,10 ).perform()
13、 针对键盘操作
action.key_down(self, value, element=None) 按下键盘某个键位,不释放
action.key_up(self, value, element=None) 释放键盘某个键位
a. Keys 属性介绍:
# 引入依赖包
from selenium.webdriver.common.keys import Keys
Keys.CONTROL Ctrl 键
Keys.ALT Alt 键
Keys.SHIFT Shift 键
Keys.TAB Tab 键
Keys.ESCAPE Esc 键
Keys.ENTER Enter 键
Keys.DELETE DEL 键
b. 使用
# 模拟键盘 Ctrl + a  全选操作
action.key_down(Keys.CONTROL).send_keys('a')

# 释放 Ctrl 键
action.key_up(Keys.CONTROL)

# 执行操作
action.perform()

猜你喜欢

转载自blog.csdn.net/weixin_38708177/article/details/88235260
今日推荐