1. 程式人生 > >webdriver(python)-- 滑鼠事件

webdriver(python)-- 滑鼠事件

測試的產品中有一個操作是右鍵點選檔案列表會彈出一個快捷選單,可以方便的選擇快捷選單中的選擇對檔案進行操作(刪除、移動、重新命名),之前學習元素的點選非常簡單:

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 的指點。

下面介紹滑鼠右鍵的用法,以快播私有云為例:

複製程式碼
#coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

driver = webdriver.Firefox()
driver.get(
"http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F") #登陸快播私有云 driver.find_element_by_id("user_name").send_keys("username") driver.find_element_by_id("user_pwd").send_keys("123456") driver.find_element_by_id("dl_an_submit").click() time.sleep(3) #定位到要右擊的元素 qqq =driver.find_element_by_xpath("
/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]") #對定位到的元素執行滑鼠右鍵操作 ActionChains(driver).context_click(qqq).perform() ''' #你也可以使用三行的寫法,但我覺得上面兩行寫法更容易理解 chain = ActionChains(driver) implement = driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]") chain.context_click(implement).perform() ''' time.sleep(3) #休眠3秒 driver.close()
複製程式碼

這裡需要注意的是,在使用ActionChains 類之前,要先將包引入。

右擊的操作會了,下面的其它方法比葫蘆畫瓢也能寫出來。

滑鼠雙擊的寫法:

#定位到要雙擊的元素
qqq =driver.find_element_by_xpath("xxx")
#對定位到的元素執行滑鼠雙擊操作
ActionChains(driver).double_click(qqq).perform()

滑鼠拖放操作的寫法:

複製程式碼
#定位元素的原位置
element = driver.find_element_by_name("source")
#定位元素要移動到的目標位置
target =  driver.find_element_by_name("target")

#執行元素的移動操作
ActionChains(driver).drag_and_drop(element, target).perform()
複製程式碼

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.

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.

  – 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.


相關推薦

webdriver(python)-- 滑鼠事件

測試的產品中有一個操作是右鍵點選檔案列表會彈出一個快捷選單,可以方便的選擇快捷選單中的選擇對檔案進行操作(刪除、移動、重新命名),之前學習元素的點選非常簡單: driver.find_element_by_id(“xxx”).click() 那麼滑鼠的雙擊、右擊、拖動

Selenium WebDriver滑鼠事件

滑鼠點選操作  滑鼠點選事件有以下幾種型別:  清單 1. 滑鼠左鍵點選   Actions action = new Actions(driver);action.click();// 滑鼠左鍵在當前停留的位置做單擊操作   action.click(driver.fin

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

本節重點: ActionChains 類   context_click()  右擊   double_click()   雙擊   drag_and_drop()  拖動 測試的產品中有一個操作是右鍵點選檔案列表會彈出一個快捷選單,可以方便的選擇快捷選單中的選擇對檔案進行操作(刪除、移

python+selenium三:滑鼠事件與鍵盤事件 python+selenium三:滑鼠事件與鍵盤事件

python+selenium三:滑鼠事件與鍵盤事件   # 1、滑鼠事件:# 每個模擬事件後需加.perform() 才會執行# context_click() 右擊# double_click() 雙擊# drag_and_drop(source, target) 拖動#

Python OpenCV _1基本操作(畫圖,迴圈播放影象,滑鼠事件,讀取中文路徑中的圖片)

此係列原始碼在我的GitHub裡:https://github.com/yeyujujishou19/Python-OpenCV 一,OpenCV的結構 A)根據功能和需求的不同,OpenCV中的函式介面大體可以分為如下部分: core 核心模組,主要包

Selenium3+webdriver學習筆記5(模擬常用鍵盤和滑鼠事件

#!/usr/bin/env python# -*- coding:utf-8 -*-from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.acti

Selenium3+webdriver學習筆記14(等待判斷 滑鼠事件

#!/usr/bin/env python# -*- coding:utf-8 -*-#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver學習筆記14(等待判斷 滑鼠事件 )'''from selenium import webdr

python+selenium 滑鼠事件操作

一、前言       除了可以使用 click( ) 來模擬滑鼠的單擊操作,現在Web產品中還提供了更豐富的滑鼠互動方式,例如滑鼠右鍵、雙擊、懸停、拖動等功能,在WebDriver中,將這些關於滑鼠操作的方法都封裝在 ActionChains 類中。

webdriver自動化測試_滑鼠事件

我們在實際的web 產品測試中發現,有關滑鼠的操作,不單單隻有單,有時候還要和到右擊,雙擊,拖動等操作,這些操作包含在ActionChains 類中。 ActionChains 類滑鼠操作的常用方法:  context_click() 右擊  doub

Python selenium的webdriver滑鼠懸停

#coding=UTF-8 from selenium import webdriver import time from selenium.webdriver.common.action_chains import ActionChains driver = webdr

selenium + python 滑鼠點選事件

對滑鼠的點選事件做了一些瞭解,從百度發現有很多相關內容 參考文章https://www.ibm.com/developerworks/cn/java/j-lo-keyboard/ 和 http://blog.csdn.net/liujingqiu/article/detai

python-Event事件線程同步和互斥

sse logs pan else 控制 事件 utf-8 event Coding 1 #!/usr/bin/python 2 #coding=utf-8 3 #用於線程間通信,通過事件標識控制 4 import threading 5 from time

Python定時事件 Timer & sched

調度 ron python 任務 我們 必須 一個 模塊 需要   我們經常需要定時的執行某一個任務,在C/C++ 等語言中我們可以使用定時器, 但是在Python中,標準庫給我們提供了兩種方式Timer和Sched。   先說sched模塊,準確的說,它是一個調度(延

selenium webdriver (python)大全

... 窗口 修改 4.0 labels 下載安裝 update 移動 定位 webdriver的簡介 硒2.0的主要新功能是集成的webdriver的API。webdriver的設計除了解決一些seleniumr-RC API的一些限制,與webdriver 的整合,將

Selenium WebDriver +Python講解

核心部分 測試用例 image 技術分享 ron 核心 其中 定義 重現 1. Selenium1.0家譜: 1.1 Selenium IDE :是嵌入到瀏覽器中的一個插件,實現簡單的瀏覽器操作的錄制和回放功能。官方給出的定義:快速的創建bug重現腳本,在測試人員測試

輕松自動化---selenium-webdriver(python) (八)

list sheet 顯示 test passport toggle home cnblogs *args http://www.testclass.net/ 測試教程網,專業的selenium 學習網站。 本節重點: 調用js方法 execute_script(scr

輕松自動化---selenium-webdriver(python) (五)

import 容易 打開 註意 選項 gif roman netd 過程 http://www.testclass.net/ 測試教程網,專業的selenium 學習網站。 本節要解決的問題: 層級定位 場景:   假如兩個控件,他們長的一模樣,還都叫“張三”,

輕松自動化---selenium-webdriver(python) (七)

see title styles min days pan 發現 doc har http://www.testclass.net/ 測試教程網,專業的selenium 學習網站。 本節知識點: 多層框架或窗口的定位: switch_to_frame() switch

輕松自動化---selenium-webdriver(python) (四)

sre utf-8 和我 school ive pat trap 自動 gpo http://www.testclass.net/ 測試教程網,專業的selenium 學習網站。 本節要解決的問題: 如何定位一組元素? 場景 從上一節的例子中可以看出,webdriv

輕松自動化---selenium-webdriver(python) (六)

import 關鍵字 百度一下 清除 attr family .cn 參考 keys http://www.testclass.net/ 測試教程網,專業的selenium 學習網站。 本節知識點: 操作對象: · click 點擊對象 · send_keys 在對象