首页 > 其他 > 详细

selenium鼠标操作

时间:2019-07-09 22:21:44      阅读:113      评论:0      收藏:0      [点我收藏+]

selenium鼠标操作

 

1.      actionchains

用selenium做自动化,有时候会遇到需要模拟鼠标操作才能进行的情况,比如单击、双击、点击鼠标右键、拖拽等等。而selenium给我们提供了一个类来处理这类事件——ActionChains

这个类基本能够满足我们所有对鼠标操作的需求。

selenium.webdriver.common.action_chains.ActionChains(driver)

 

当然在引用时一般用from selenium.webdriver import ActionChains

但实质上它位于from .common.action_chains import ActionChains  # noqa

 

1.1.    ActionChains基本用法

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

 

这种情况下可以有两种调用方法:

 

1.链式

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()

 

2.分步式

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.2.    ActionChains方法列表

click(on_element=None) ——单击鼠标左键

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) ——发送某个键到指定元素

 

selenium鼠标操作

原文:https://www.cnblogs.com/wodeboke-y/p/11160801.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!