Releasing a held mouse button on an element.
:Args:
- on_element: The element to mouse up.
If None, releases on current mouse position.
"""
if self._driver.w3c:
self.w3c_actions.pointer_action.release()
self.w3c_actions.key_action.pause()
else:
if on_element:
self.move_to_element(on_element)
self._actions.append(lambda: self._driver.execute(Command.MOUSE_UP, {}))
return self
def send_keys(self, *keys_to_send):
"""
Sends keys to current focused element.
:Args:
- keys_to_send: The keys to send. Modifier keys constants can be found in the
'Keys' class.
"""
if self._driver.w3c:
self.w3c_actions.key_action.send_keys(keys_to_send)
else:
self._actions.append(lambda: self._driver.execute(
Command.SEND_KEYS_TO_ACTIVE_ELEMENT, {'value': keys_to_typing(keys_to_send)}))
return self
def send_keys_to_element(self, element, *keys_to_send):
"""
Sends keys to an element.
:Args:
- element: The element to send keys.
- keys_to_send: The keys to send. Modifier keys constants can be found in the
'Keys' class.
"""
if self._driver.w3c:
self.w3c_actions.key_action.send_keys(keys_to_send, element=element)
else:
self._actions.append(lambda: element.send_keys(*keys_to_send))
return self
# Context manager so ActionChains can be used in a 'with .. as' statements.
def __enter__(self):
return self # Return created instance of self.
def __exit__(self, _type, _value, _traceback):
pass # Do nothing, does not require additional cleanup.
方法列表
perform(self): ---執(zhí)行鏈中的所有動(dòng)作
reset_actions(self): ---清除存儲(chǔ)在遠(yuǎn)端的動(dòng)作
click(self, on_element=None): ---鼠標(biāo)左鍵單擊
click_and_hold(self, on_element=None): --鼠標(biāo)左鍵單擊,不松開
context_click(self, on_element=None): ---鼠標(biāo)右鍵單擊
double_click(self, on_element=None): ---鼠標(biāo)左鍵雙擊
drag_and_drop(self, source, target): ---拖拽到某個(gè)元素后松開
drag_and_drop_by_offset(self, source, xoffset, yoffset): ---拖拽到某個(gè)坐標(biāo)后松開
key_down(self, value, element=None): ---某個(gè)鍵盤鍵被按下
key_up(self, value, element=None): ---松開某個(gè)鍵
move_by_offset(self, xoffset, yoffset): ---鼠標(biāo)移動(dòng)到某個(gè)坐標(biāo)
move_to_element(self, to_element): ---鼠標(biāo)移動(dòng)到某個(gè)元素
move_to_element_with_offset(self, to_element, xoffset, yoffset): ---移動(dòng)到距某個(gè)元素(左上角)多少的位置
release(self, on_element=None): ---在某元素上松開鼠標(biāo)
send_keys(self, *keys_to_send): ---發(fā)送某些值到當(dāng)前焦點(diǎn)元素
send_keys_to_element(self, element, *keys_to_send): ---發(fā)送某些值到指定元素
基本用法
鏈?zhǔn)綄懛?br />
ActionChains(driver).click(clk_btn).context_click(right_btn).perform()
分步寫法
# 補(bǔ)全化action
actions = ActionChains(driver)
# 裝載單擊動(dòng)作
actions.click()
# 裝載右擊動(dòng)作
actions.context_click()
# 執(zhí)行所有被裝載的動(dòng)作
actions.perform()
應(yīng)用舉例
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("
https://www.baidu.com/")
driver.implicitly_wait(10)
# 右擊百度新聞
right_click = driver.find_element_by_xpath('//a[@name="tj_trnews"]')
ActionChains(driver).context_click(right_click).perform()