Python+Selenium自动化测试 4. ActionChains鼠标,键盘事件
目录
7. drag_and_drop(source,target) # 拖拽到某个元素后松开
8.drag_and_drop_by_offset #拖拽到某个坐标后松开
10. move_to_element #鼠标移动到某个元素
11. move_to_element_with_offset #移动到距某个元素(左上角)多少的位置
5 send_keys_to_element #发送某些值到指定元素
一 ActionChains介绍
源码:
ActionChains是一种低级别的自动化交互方法,例如鼠标移动,鼠标按钮操作,按键操作和上下文菜单交互。这对于执行更复杂的操作非常有用,例如悬停和拖放。
生成用户操作。
在ActionChains对象上调用操作方法时,
操作存储在ActionChains对象的队列中。
当您调用perform()时,事件将按它们的顺序触发
排队等候。
ActionChains可以用于链式::
menu = driver.find_element_by_css_selector(“.nav”)
hidden_submenu = driver.find_element_by_css_selector(“.nav#submenu1”)
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
或者操作可以逐个排队,然后执行。::
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
1. 引入方式
from selenium.webdriver.common.action_chains import ActionChains
2. 实现原理
实际上ActionChains这个模块的实现的核心思想就是,当你调用ActionChains的方法时,不会立即执行,而是会将所有的操作按顺序存放在一个List里,当你调用perform()方法时,队列中的时间会依次执行
3. 基本用法
链式写法
ActionChains(driver).click(clk_btn).context_click(right_btn).perform()
分步写法
# 补全化action
actions = ActionChains(driver)
# 装载单击动作
actions.click()
# 装载右击动作
actions.context_click()
# 执行所有被装载的动作
actions.perform()
4. 举个栗子
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains # 引入 ActionChains 类
driver = webdriver.Firefox()
driver.get("https://www.baidu.com")
# 定位到要右击的元素
right_click = driver.find_element_by_id("xxxx")
# 对定位到的元素执行鼠标右键操作
ActionChains(driver).context_click(right_click).perform()
1)ActionChains(driver)
调用 ActionChains() 类,将浏览器驱动 driver 作为参数传入。
2)c(right_click)
context_click() 方法用于模拟鼠标右击操作,在调用时需要指定元素定位。
3)perform()
执行所有 ActionChains 中存储的行为,可以理解为是对整个操作的提交动作。
5. API汇总
perform(self): ---执行链中的所有动作
reset_actions(self): ---清除存储在远端的动作
click(self, on_element=None): ---鼠标左键单击
click_and_hold(self, on_element=None): --鼠标左键单击,不松开
context_click(self, on_element=None): ---鼠标右键单击
double_click(self, on_element=None): ---鼠标左键双击
drag_and_drop(self, source, target): ---拖拽到某个元素后松开
drag_and_drop_by_offset(self, source, xoffset, yoffset): ---拖拽到某个坐标后松开
key_down(self, value, element=None): ---某个键盘键被按下
key_up(self, value, element=None): ---松开某个键
move_by_offset(self, xoffset, yoffset): ---鼠标移动到某个坐标
move_to_element(self, to_element): ---鼠标移动到某个元素
move_to_element_with_offset(self, to_element, xoffset, yoffset): ---移动到距某个元素(左上角)多少的位置
release(self, on_element=None): ---在某元素上松开鼠标
send_keys(self, *keys_to_send): ---发送某些值到当前焦点元素
send_keys_to_element(self, element, *keys_to_send): ---发送某些值到指定元素
二 鼠标操作
详细API介绍
1. perform # 执行链中的所有动作
2. reset_actions #清除存储在远端的动作
3.click #鼠标左键单击
栗子:
ActionChains(driver).click()
4.click_and_hold #鼠标左键单击,不松开
栗子:
ActionChains(driver).click_and_hold()
5.context_click #鼠标右键单击
栗子:
ActionChains(driver).context_click()
6.double_click #鼠标左键双击
栗子:
# 定位到要双击的元素
double_click = driver.find_element_by_id("xx")
# 对定位到的元素执行悬停操作
ActionChains(driver).double_click(double_click).perform()
7. drag_and_drop(source,target) # 拖拽到某个元素后松开
看源码解释,在源元素source上按住鼠标左键,然后移动到目标元素target上释放。
- source:鼠标拖动的源元素。
- target:鼠标释放的目标元素。
栗子:
# 定位到元素的原位置
element = driver.find_element_by_id("xx")
# 定位到元素要移动到的目标位置
target = driver.find_element_by_id("xx")
# 执行元素的拖放操作
ActionChains(driver).drag_and_drop(element,target).perform()
8.drag_and_drop_by_offset #拖拽到某个坐标后松开
栗子:
# 将一个source元素 拖动到针对source坐上角坐在的x y处 可存在负宽度的情况和负高度的情况
ActionChains(driver).drag_and_drop_by_offset(source, x, y)
9. move_by_offset #鼠标移动到某个坐标
栗子:
# 这种也是拖拽的一种方式,都是以源元素的左上角为基准,移动坐标
ActionChains(driver).click_and_hold(dom).move_by_offset(169,188).release().perform()
ActionChains(driver).move_by_offset(a['x'],a['y']).double_click(dis).perform()
10. move_to_element #鼠标移动到某个元素
栗子:
ActionChains(driver).move_to_element(e)
11. move_to_element_with_offset #移动到距某个元素(左上角)多少的位置
12. pause
13. release #在某元素上松开鼠标左键
栗子:
ActionChains(driver).release()
三 键盘操作
keys 可以配合 key_down,和key_up ,做一些键盘上的操作
1. 引入包:
from selenium.webdriver.common.keys import Keys
2. key_down #某个键盘键被按下
3. key_up # 松开某个键
4. send_keys #发送某些值到当前焦点元素
5 send_keys_to_element #发送某些值到指定元素
6. keys
栗子:
# key_down 模拟键盘摁下某个按键 key_up 松开某个按键,与sendkey连用完成一些操作,每次down必须up一次否则将出现异常
ActionChains(driver).key_down(Keys.CONTROL,dom).send_keys('a').send_keys('c').key_up(Keys.CONTROL)\
.key_down(Keys.CONTROL,dom1).send_keys('v').key_up(Keys.CONTROL).perform()
#输入框输入内容
driver.find_element_by_id("kw").send_keys("seleniumm")
#删除多输入的一个 m
driver.find_element_by_id("kw").send_keys(Keys.BACK_SPACE)
#输入空格键+“教程”
driver.find_element_by_id("kw").send_keys(Keys.SPACE)
driver.find_element_by_id("kw").send_keys(u"教程")
#ctrl+a 全选输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
#ctrl+x 剪切输入框内容
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')
#ctrl+v 粘贴内容到输入框
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'v')
#通过回车键盘来代替点击操作
driver.find_element_by_id("su").send_keys(Keys.ENTER)
#回退键 ESC
driver.find_element_by_id("su").send_keys(Keys.ESCAPE)
send_keys(Keys.BACK_SPACE) 删除键(BackSpace) send_keys(Keys.SPACE) 空格键(Space) send_keys(Keys.TAB) 制表键(Tab) send_keys(Keys.ESCAPE) 回退键(Esc) send_keys(Keys.ENTER) 回车键(Enter) send_keys(Keys.CONTROL,'a') 全选(Ctrl+A) send_keys(Keys.CONTROL,'c') 复制(Ctrl+C) send_keys(Keys.CONTROL,'x') 剪切(Ctrl+X) send_keys(Keys.CONTROL,'v') 粘贴(Ctrl+V) send_keys(Keys.F1) 键盘 F1 send_keys(Keys.F12) 键盘 F12
ALL keys:
class Keys(object): """ Set of special keys codes. """ NULL = '\ue000' CANCEL = '\ue001' # ^break HELP = '\ue002' BACKSPACE = '\ue003' BACK_SPACE = BACKSPACE TAB = '\ue004' CLEAR = '\ue005' RETURN = '\ue006' ENTER = '\ue007' SHIFT = '\ue008' LEFT_SHIFT = SHIFT CONTROL = '\ue009' LEFT_CONTROL = CONTROL ALT = '\ue00a' LEFT_ALT = ALT PAUSE = '\ue00b' ESCAPE = '\ue00c' SPACE = '\ue00d' PAGE_UP = '\ue00e' PAGE_DOWN = '\ue00f' END = '\ue010' HOME = '\ue011' LEFT = '\ue012' ARROW_LEFT = LEFT UP = '\ue013' ARROW_UP = UP RIGHT = '\ue014' ARROW_RIGHT = RIGHT DOWN = '\ue015' ARROW_DOWN = DOWN INSERT = '\ue016' DELETE = '\ue017' SEMICOLON = '\ue018' EQUALS = '\ue019' NUMPAD0 = '\ue01a' # number pad keys NUMPAD1 = '\ue01b' NUMPAD2 = '\ue01c' NUMPAD3 = '\ue01d' NUMPAD4 = '\ue01e' NUMPAD5 = '\ue01f' NUMPAD6 = '\ue020' NUMPAD7 = '\ue021' NUMPAD8 = '\ue022' NUMPAD9 = '\ue023' MULTIPLY = '\ue024' ADD = '\ue025' SEPARATOR = '\ue026' SUBTRACT = '\ue027' DECIMAL = '\ue028' DIVIDE = '\ue029' F1 = '\ue031' # function keys F2 = '\ue032' F3 = '\ue033' F4 = '\ue034' F5 = '\ue035' F6 = '\ue036' F7 = '\ue037' F8 = '\ue038' F9 = '\ue039' F10 = '\ue03a' F11 = '\ue03b' F12 = '\ue03c' META = '\ue03d' COMMAND = '\ue03d'