selenium-webdriver(python) -- 鼠標事件(雙擊,右鍵)

本節(jié)重點:

ActionChains?類

??context_click()??右擊

??double_click()???雙擊

??drag_and_drop()??拖動


測試的產品中有一個操作是右鍵點擊文件列表會彈出一個快捷菜單怔檩,可以方便的選擇快捷菜單中的選擇對文件進行操作(刪除揽浙、移動冬念、重命名)提前,之前學習元素的點擊非常簡單:

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

那么鼠標的雙擊、右擊、拖動等是否也是這樣的寫法呢券敌?例如右擊:

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

經(jīng)過運行腳本得到了下面的錯誤提示:

AttributeError:?'WebElement'?object?has?no?attribute?'context_click'?

提示右點方法不屬于webelement?對象唾戚,通過查找文檔,發(fā)現(xiàn)屬于ActionChains?類待诅,但文檔中沒有具體寫法叹坦。這里要感謝?北京-QC-rabbit?的指點,其實整個python+selenium?學習過程都要感謝?北京-QC-rabbit?的指點卑雁。



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

#coding=utf-8fromseleniumimport webdriverfromselenium.webdriver.common.action_chainsimport ActionChainsimport 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]")#對定位到的元素執(zhí)行鼠標右鍵操作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")#對定位到的元素執(zhí)行鼠標雙擊操作ActionChains(driver).double_click(qqq).perform()



鼠標拖放操作的寫法:

#定位元素的原位置element = driver.find_element_by_name("source")#定位元素要移動到的目標位置target =? driver.find_element_by_name("target")#執(zhí)行元素的移動操作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.

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末琉挖,一起剝皮案震驚了整個濱河市启泣,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌示辈,老刑警劉巖寥茫,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異矾麻,居然都是意外死亡纱耻,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門险耀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來弄喘,“玉大人,你說我怎么就攤上這事甩牺∠薮危” “怎么了?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵柴灯,是天一觀的道長。 經(jīng)常有香客問我费尽,道長赠群,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任旱幼,我火速辦了婚禮查描,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己冬三,他們只是感情好匀油,可當我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著勾笆,像睡著了一般敌蚜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上窝爪,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天弛车,我揣著相機與錄音,去河邊找鬼蒲每。 笑死纷跛,一個胖子當著我的面吹牛,可吹牛的內容都是我干的邀杏。 我是一名探鬼主播贫奠,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼望蜡!你這毒婦竟也來了唤崭?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤泣特,失蹤者是張志新(化名)和其女友劉穎浩姥,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體状您,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡勒叠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了膏孟。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片眯分。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖柒桑,靈堂內的尸體忽然破棺而出弊决,到底是詐尸還是另有隱情,我是刑警寧澤魁淳,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布飘诗,位于F島的核電站,受9級特大地震影響界逛,放射性物質發(fā)生泄漏昆稿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一息拜、第九天 我趴在偏房一處隱蔽的房頂上張望溉潭。 院中可真熱鬧净响,春花似錦、人聲如沸喳瓣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽畏陕。三九已至配乓,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蹭秋,已是汗流浹背扰付。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留仁讨,地道東北人羽莺。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像洞豁,于是被迫代替她去往敵國和親盐固。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內容