天天看点

selenium-webdriver(python) (十五) -- 鼠标事件

本节重点:

actionchains 类

  context_click()  右击

  double_click()   双击

  drag_and_drop()  拖动

测试的产品中有一个操作是右键点击文件列表会弹出一个快捷菜单,可以方便的选择快捷菜单中的选择对文件进行操作(删除、移动、重命名),之前学习元素的点击非常简单:

driver.find_element_by_id(“xxx”).click()

那么鼠标的双击、右击、拖动等是否也是这样的写法呢?例如右击:

driver.find_element_by_id(“xxx”).context_click()

经过运行脚本得到了下面的错误提示:

attributeerror: 'webelement' object has no attribute 'context_click' 

提示右点方法不属于webelement 对象,通过查找文档,发现属于actionchains 类,但文档中没有具体写法。这里要感谢 北京-qc-rabbit 的指点,其实整个python+selenium 学习过程都要感谢 北京-qc-rabbit 的指点。

下面介绍鼠标右键的用法,以快播私有云为例:

这里需要注意的是,在使用actionchains 类之前,要先将包引入。

右击的操作会了,下面的其它方法比葫芦画瓢也能写出来。

鼠标双击的写法:

鼠标拖放操作的写法:

actionchains 类不仅仅是只包含了上面的三个方法,下面将方法列出:

class actionchains(driver)

driver:the webdriver instance which performs user actions.

generate user actions. all actions are stored in the actionchains object. call perform() to fire stored actions.

  – perform()

performs all stored actions.

  – click(on_element=none)

clicks an element.

on_element:the element to click. if none, clicks on current mouse position.

  – click_and_hold(on_element)

holds down the left mouse button on an element.

on_element:the element to mouse down. if none, clicks on current mouse position.

  – context_click(on_element)

performs a context-click (right click) on an element.

on_element:the element to context-click. if none, clicks on current mouse position.

  – double_click(on_element)

double-clicks an element.

on_element:the element to double-click. if none, clicks on current mouse position.

  

  – drag_and_drop(source, target)

holds down the left mouse button on the source element, then moves to the target element and releases the mouse button.

source:the element to mouse down.

target: the element to mouse up.

  – key_down(key, element=none)

sends a key press only, without releasing it. should only be used with modifier keys (control, alt andshift).

key:the modifier key to send. values are defined in keys class.

element:the element to send keys. if none, sends a key to current focused element.

  – key_up(key, element=none)

releases a modifier key.

  – move_by_offset(xoffset, yoffset)

moving the mouse to an offset from current mouse position.

xoffset:x offset to move to.yoffset:y offset to move to.

  – move_to_element(to_element)

moving the mouse to the middle of an element.

to_element: the element to move to.

  – move_to_element_with_offset(to_element, xoffset, yoffset)

move the mouse by an offset of the specificed element. offsets are relative to the top-left corner of the

element.

to_element: the element to move to.xoffset:x offset to move to.yoffset:y offset to move to.

  – release(on_element)

releasing a held mouse button.

on_element:the element to mouse up.

  – send_keys(*keys_to_send)

sends keys to current focused element.

keys_to_send:the keys to send.

  – send_keys_to_element(self, element,*keys_to_send):

sends keys to an element.

element:the element to send keys.keys_to_send:the keys to send.