Selenium3 Python WebDriver API源碼探析(10):動(dòng)作鏈(ActionChains):鼠標(biāo)事件和
鼠標(biāo)、鍵盤(pán)事件是我們利用Selenium操控瀏覽器的重要交互手段,主要由selenium\webdriver\common\action_chains.py中的ActionChains類實(shí)現(xiàn)。該類通過(guò)webdriver包對(duì)外暴露,即可使用from selenium.webdriver import ActionChains導(dǎo)入ActionChains類。
動(dòng)作鏈(ActionChains)原理
動(dòng)作鏈?zhǔn)且环N低級(jí)交互方法,如鼠標(biāo)移動(dòng)、鼠標(biāo)按鈕操作、按鍵按下和上下文菜單交互。動(dòng)作鏈對(duì)于復(fù)雜動(dòng)作非常有效,比如懸停和拖放。
ActionChains類提供了兩類方法:
動(dòng)作鏈基礎(chǔ)方法
各種動(dòng)作方法
ActionChains類在實(shí)例化時(shí),生成了兩個(gè)類屬性,一個(gè)為_(kāi)driver,即WebDriver實(shí)例,另一個(gè)為_(kāi)actions,初始為空列表,隨后ActionChains實(shí)例在調(diào)用其他動(dòng)作方法時(shí),會(huì)先把需要執(zhí)行的動(dòng)作存儲(chǔ)在_actions中,當(dāng)ActionChains實(shí)例調(diào)用perform()方法時(shí),才會(huì)真正執(zhí)行動(dòng)作。
動(dòng)作鏈的調(diào)用模式有兩種:
鏈?zhǔn)秸Z(yǔ)法
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
命令隊(duì)列
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
ActionChains類動(dòng)作鏈基礎(chǔ)方法
perform()方法
作用:依次執(zhí)行動(dòng)作鏈中的每個(gè)動(dòng)作。
reset_actions()方法
作用:清空待執(zhí)行的動(dòng)作鏈。
ActionChains類鼠標(biāo)操作方法
move_to_element方法
作用:將鼠標(biāo)從當(dāng)前位置移動(dòng)至某元素中心位置。
方法簽名:ActionChains(driver).move_to_element(to_element)
參數(shù):on_element,鼠標(biāo)移動(dòng)的目標(biāo)元素。
click方法
作用:將鼠標(biāo)移動(dòng)至給定元素,然后點(diǎn)擊該元素。
方法簽名:ActionChains(driver).click(on_element=None)
參數(shù):on_element,即將點(diǎn)擊的元素,如果參數(shù)為None,點(diǎn)擊鼠標(biāo)當(dāng)前位置。
原理:先執(zhí)行move_to_element方法,再點(diǎn)擊元素。
click_and_hold方法
作用:將鼠標(biāo)移動(dòng)至給定元素,然后在給定元素中間單擊(不釋放)。
方法簽名:ActionChains(driver).click_and_hold(on_element=None)
參數(shù):on_element,鼠標(biāo)按下的元素,如果參數(shù)為None,點(diǎn)擊鼠標(biāo)當(dāng)前位置。
context_click方法
作用:將鼠標(biāo)移動(dòng)至給定元素,然后用鼠標(biāo)右鍵單擊該元素。
方法簽名:ActionChains(driver).context_click(on_element=None)
參數(shù):on_element,鼠標(biāo)右鍵單擊的元素,如果參數(shù)為None,鼠標(biāo)右鍵點(diǎn)擊當(dāng)前位置。
原理:先執(zhí)行move_to_element方法,再右鍵點(diǎn)擊元素。
double_click方法
作用:將鼠標(biāo)移動(dòng)至給定元素,然后用鼠標(biāo)雙擊該元素。
方法簽名:ActionChains(driver).double_click(on_element=None)
參數(shù):on_element,鼠標(biāo)雙擊的元素,如果參數(shù)為None,鼠標(biāo)雙擊當(dāng)前位置。
原理:先執(zhí)行move_to_element方法,再雙擊元素。
release方法
作用:釋放按在某元素上的鼠標(biāo)。
方法簽名:ActionChains(driver).release(on_element=None)
參數(shù):on_element,釋放鼠標(biāo)的元素,如果參數(shù)為None,在當(dāng)前位置釋放鼠標(biāo)。
move_by_offset方法
作用:將鼠標(biāo)從當(dāng)前位置按偏移量移動(dòng)鼠標(biāo)。
方法簽名:ActionChains(driver).move_by_offset(xoffset, yoffset)
參數(shù):
xoffset:需要移動(dòng)的X偏移量。正負(fù)整數(shù)。
yoffset:需要移動(dòng)的Y偏移量。正負(fù)整數(shù)。
move_to_element_with_offset方法
作用:將鼠標(biāo)從當(dāng)前位置移動(dòng)至某元素中心位置(按偏移量移動(dòng),偏移量相對(duì)于元素的左上角)。
方法簽名:ActionChains(driver).move_to_element_with_offset(to_element, xoffset, yoffset)
參數(shù):
to_element:鼠標(biāo)移動(dòng)的目標(biāo)元素
xoffset:需要移動(dòng)的X偏移量
yoffset:需要移動(dòng)的Y偏移量
drag_and_drop方法
作用:拖放元素(按下鼠標(biāo)左鍵拖動(dòng)源元素至目標(biāo)元素時(shí)釋放鼠標(biāo)左鍵)。
方法簽名:ActionChains(driver).drag_and_drop(source, target)
參數(shù):
source:鼠標(biāo)拖動(dòng)的元素
target:鼠標(biāo)釋放的元素
原理:先click_and_hold(source),再release(target)
drag_and_drop_by_offset方法
作用:拖放元素,類似于 drag_and_drop方法,只不過(guò)將元素拖放某些偏移量。
方法簽名:ActionChains(driver).drag_and_drop_by_offset(source, xoffset, yoffset)
參數(shù):
source:鼠標(biāo)拖動(dòng)的元素
xoffset:需要移動(dòng)的X偏移量
yoffset:需要移動(dòng)的Y偏移量
原理:先click_and_hold(source),再move_by_offset(xoffset, yoffset),最后release()
ActionChains類鍵盤(pán)操作方法
Selenium不但可以輸入實(shí)體字符,還可以輸入組合鍵。selenium\webdriver\common\keys.py中的Keys類映射了大多數(shù)修飾鍵。可使用from selenium.webdriver.common.keys import Keys導(dǎo)入Keys類。
key_down方法
作用:按下某個(gè)鍵(不釋放),應(yīng)當(dāng)只用于修飾鍵(CTRL、ALT、SHIFT)。
方法簽名:ActionChains(driver).key_down(value, element=None)
參數(shù):
value:按下的修飾鍵。
element:按鍵的元素,如果參數(shù)為None,在當(dāng)前位置按鍵。
key_up方法
作用:釋放某個(gè)修飾鍵。
方法簽名:ActionChains(driver).key_up(value, element=None)
參數(shù):
value:按下的修飾鍵。
element:按鍵的元素,如果參數(shù)為None,在當(dāng)前位置按鍵。
案例:按下ctrl+c
ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
send_keys方法
作用:向當(dāng)前元素輸入內(nèi)容。
方法簽名:ActionChains(driver).send_keys(*keys_to_send)
參數(shù): keys_to_send:即將發(fā)送的內(nèi)容。
send_keys_to_element方法
作用:向某元素輸入內(nèi)容。
方法簽名:ActionChains(driver).send_keys_to_element(element, *keys_to_send)
參數(shù):
element:給定元素。
keys_to_send:即將發(fā)送的內(nèi)容。
原理:先click(element),再send_keys(*keys_to_send)
pause方法
作用:按秒數(shù)暫停所有輸入。
方法簽名:ActionChains(driver).pause(seconds)
參數(shù):seconds暫停秒數(shù)。