Selenium+python automation 94-action event (ActionChains) source code detailed

Introduction to ActionChains

Actionchains are specialized in dealing with mouse-related operations in selenium, such as: mouse movement, mouse button operation, button and context menu (right mouse button) interaction.
This is useful for doing more complex actions like hovering and dragging and dropping.

Actionchains can also be combined with shortcut keys, such as ctrl, shift, alt combined with the mouse

When you use actionchains object methods, action events are stored in the actionchains object queue. When you use perform(), events are executed sequentially.

  • Method 1: You can write a long string
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()
  • Method 2: You can write in several steps
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()

Either way, the operations are executed one after the other in the order they were called!

Method introduction

1. The Keys class mentioned below is the keyboard event class in selenium, and the import method is:

from selenium.webdriver.common.keys import Keys

2. Mouse events use the import method:

from selenium.webdriver.common.action_chains import ActionChains

class ActionChains(object):
    def __init__(self, driver): self._driver = driver self._actions = [] def perform(self): # 执行行为事件 def click(self, on_element=None): 点击: - 如果参数不写,那么点击的是当前鼠标位置 - 如果参数写定位到的元素对象element,那就是点这个元素 def click_and_hold(self, on_element=None): 鼠标左键按住某个元素 - 如果参数不写,那么点的是当前鼠标位置 - 如果参数写定位到的元素对象element,那就是点这个元素 def context_click(self, on_element=None): 鼠标右键点击 - 如果参数不写,那么点的是当前鼠标位置 - 如果参数写定位到的元素对象element,那就是点这个元素 def double_click(self, on_element=None): 双击鼠标 - 如果参数不写,那么点的是当前鼠标位置 - 如果参数写定位到的元素对象element,那就是点这个元素 def drag_and_drop(self, source, target): 按住源元素上的鼠标左键,然后移动到目标元素并释放鼠标按钮 - source: 按住鼠标的元素位置 - target: 松开鼠标的元素位置 def drag_and_drop_by_offset(self, source, xoffset, yoffset): 按住源元素上的鼠标左键,然后移动到目标偏移量并释放鼠标按钮。 - source: 按住鼠标的元素位置 - xoffset: X 轴的偏移量 - yoffset: Y 轴的偏移量 def key_down(self, value, element=None): 只发送一个按键,而不释放它。只应用于修饰键(控制、alt和shift)。 - value: 要发送的修饰符键。值在“Keys”类中定义。 - element: 定位的元素 如果element参数不写就是当前鼠标的位置 举个例子,按住 ctrl+c:: ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform() def key_up(self, value, element=None): # 释放按键,配合上面的一起使用 def move_by_offset(self, xoffset, yoffset): 将鼠标移动到当前鼠标位置的偏移量 - xoffset: X轴 作为一个正整数或负整数移动到x偏移量 - yoffset: Y轴 偏移,作为正整数或负整数。 def move_to_element(self, to_element): 鼠标悬停 - to_element: 定位需要悬停的元素 def move_to_element_with_offset(self, to_element, xoffset, yoffset): 通过指定元素的偏移量移动鼠标。偏移量与元素的左上角相对 - to_element: 定位需要悬停的元素 - xoffset: X 轴偏移量 - yoffset: Y 轴偏移量 def release(self, on_element=None): 释放一个元素上的鼠标按钮。 - 如果参数不写,那么是当前鼠标位置 - 如果参数写定位到的元素对象element,那就是这个元素. def send_keys(self, *keys_to_send): 发送到当前焦点元素 要发送的按键。修饰符键常数可以在“Keys”类。 def send_keys_to_element(self, element, *keys_to_send): 发送到定位到的元素上 - element: 定位的元素 - keys_to_send: 要发送的按键。修饰符键常数可以在“Keys”类。

Take a case

1. Implement the key combination function of Ctrl + F5

# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys import time driver = webdriver.Firefox() driver.get("https://www.baidu.com") time.sleep(3) # 实现Ctrl+F5刷新 ActionChains(driver).key_down(Keys.CONTROL).send_keys(Keys.F5).key_up(Keys.CONTROL).perform()

The source code can be viewed in the following directory: Lib\site-packages\selenium\webdriver\common\action_chains.py

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325341473&siteId=291194637