天天看點

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.