您的位置:软件测试 > 开源软件测试 > 开源功能测试工具 > Selenium
Selenium模拟鼠标操作
作者:孔扎根 发布时间:[ 2017/4/11 11:36:27 ] 推荐标签:功能测试工具 Selenium

Releasing a held mouse button on an element.
:Args:
- on_element: The element to mouse up.
If None, releases on current mouse position.
"""
if self._driver.w3c:
self.w3c_actions.pointer_action.release()
self.w3c_actions.key_action.pause()
else:
if on_element:
self.move_to_element(on_element)
self._actions.append(lambda: self._driver.execute(Command.MOUSE_UP, {}))
return self
def send_keys(self, *keys_to_send):
"""
Sends keys to current focused element.
:Args:
- keys_to_send: The keys to send.  Modifier keys constants can be found in the
'Keys' class.
"""
if self._driver.w3c:
self.w3c_actions.key_action.send_keys(keys_to_send)
else:
self._actions.append(lambda: self._driver.execute(
Command.SEND_KEYS_TO_ACTIVE_ELEMENT, {'value': keys_to_typing(keys_to_send)}))
return self
def send_keys_to_element(self, element, *keys_to_send):
"""
Sends keys to an element.
:Args:
- element: The element to send keys.
- keys_to_send: The keys to send.  Modifier keys constants can be found in the
'Keys' class.
"""
if self._driver.w3c:
self.w3c_actions.key_action.send_keys(keys_to_send, element=element)
else:
self._actions.append(lambda: element.send_keys(*keys_to_send))
return self
# Context manager so ActionChains can be used in a 'with .. as' statements.
def __enter__(self):
return self  # Return created instance of self.
def __exit__(self, _type, _value, _traceback):
pass  # Do nothing, does not require additional cleanup.
  方法列表
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):     ---发送某些值到指定元素
  基本用法
  链式写法
  ActionChains(driver).click(clk_btn).context_click(right_btn).perform()
  分步写法
# 补全化action
actions = ActionChains(driver)
# 装载单击动作
actions.click()
# 装载右击动作
actions.context_click()
# 执行所有被装载的动作
actions.perform()
  应用举例
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.implicitly_wait(10)
# 右击百度新闻
right_click = driver.find_element_by_xpath('//a[@name="tj_trnews"]')
ActionChains(driver).context_click(right_click).perform()


 

上一页12下一页
软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd