10_Simulate mouse and keyboard operations

When using selenium for automation, sometimes you will encounter situations where you need to simulate mouse operations, such as clicking, double-clicking, right-clicking, dragging, etc. Selenium provides us with a class to handle such events-ActionChains

selenium.webdriver.common.action_chains.ActionChains(driver)

This class can basically meet all our needs for mouse operations.

1. Basic usage of ActionChains.
First, you need to understand the execution principle of ActionChains. When you call the method of ActionChains, it will not be executed immediately. Instead, all operations will be stored in a queue in order. When you call the perform() method, The times in the queue will be executed sequentially.
In this case we have two calling methods:

  • Chain writing
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()
  • Step by step writing method
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()

2.ActionChains method list

  • click(on_element=None) - click the left mouse button
  • click_and_hold(on_element=None) - click the left mouse button and do not release it
  • context_click(on_element=None) - click the right mouse button
  • double_click(on_element=None) - double click the left mouse button
  • drag_and_drop(source, target) - Drag to an element and release
  • drag_and_drop_by_offset(source, xoffset, yoffset) - drag to a certain coordinate and release
  • key_down(value, element=None) - press a key on the keyboard
  • key_up(value, element=None) - Release a key
  • move_by_offset(xoffset, yoffset) - moves the mouse from the current position to a certain coordinate
  • move_to_element(to_element) - Move the mouse to an element
  • move_to_element_with_offset(to_element, xoffset, yoffset) ——Move to the distance from an element (upper left corner coordinate)
  • perform() - executes all actions in the chain
  • release(on_element=None) - release the left mouse button at an element position
  • send_keys(*keys_to_send) – Send a key to the currently focused element
  • send_keys_to_element(element, *keys_to_send) - Send a key to the specified element

3. Code examples

  • click operation
ActionChains(driver).click(click_btn).perform() # 单击按钮
ActionChains(driver).double_click(doubleclick_btn).perform() # 双击按钮
ActionChains(driver).context_click(rightclick_btn).perform()  # 右键菜单
  • mouse movement
action.move_to_element(more).perform()  #移动到指定元素
action.move_by_offset(200, 100).perform()  #从当前位置移动指定的距离
action.move_to_element_with_offset(more, 870, -4).perform()  #从指定元素位置开始移动指定的距离
  • drag
'''定位元素的源位置'''
element_record = driver.find_elements_by_xpath("//td[text()='日志']")[1]
element_knowledge = driver.find_element_by_xpath("//td[text()='知识']")
element_opp = driver.find_elements_by_xpath("//td[text()='商机']")[1]
element_pro = driver.find_elements_by_xpath("//td[text()='产品']")[1]

'''定位元素要移动到的目标位置'''
target= driver.find_element_by_xpath("//td[text()='此位置未添加菜单']")

'''将日志元素拖拽到目标位置'''
ActionChains(driver).click_and_hold(element_record).perform() # 在日志元素上点击鼠标左键,不松开
ActionChains(driver).move_by_offset(2, 20).perform() # 鼠标相对于当前位置移动一定距离
ActionChains(driver).move_by_offset(2, 20).perform() # 鼠标相对于当前位置移动一定距离
ActionChains(driver).move_to_element(target).perform() # 鼠标移动到目标位置
ActionChains(driver).release(element_record).perform() # 释放鼠标左键
# ActionChains(driver).drag_and_drop(element_record,target).perform()# 与上面5步操作结果相同

'''2.将知识元素拖拽到目标位置'''
ActionChains(driver).click_and_hold(element_knowledge).release(target).perform()

'''3.将商机元素拖拽到目标位置'''
ActionChains(driver).click_and_hold(element_opp).move_to_element(target).release().perform()

'''4.将产品元素拖拽到目标位置'''
# action.drag_and_drop_by_offset(element_pro, -600, 30).perform()
ActionChains(driver).click_and_hold(element_pro).move_by_offset(-600, 30).release().perform()
  • There are many ways to simulate buttons
    , which can be implemented using win32api, SendKeys, or the send_keys() method of selenium's WebElement object. Here, the ActionChains class also provides several methods for simulating buttons.
action.key_down(Keys.COMMAND).send_keys('a').key_up(Keys.COMMAND).perform()  # ctrl+a 全选
action.key_down(Keys.COMMAND).send_keys('c').key_up(Keys.COMMAND).perform()  # ctrl+c 复制
action.key_down(Keys.COMMAND).send_keys('v').key_up(Keys.COMMAND).perform()  # ctrl+v

Commonly used keys

  • Keys.BACK_SPACE #Delete key–Backspace
  • Keys.SPACE #Space key Space
  • Keys.TAB #Tab key Tab
  • Keys.ESCAPE #Escape key ESC
  • Keys.ENTER #Enter key
  • Keys.F5 #Keyboard F5 (can simulate F1-F12)
  • Keys.CONTROL #Ctrl key
  • Keys.SHIFT #shift key
  • Keys.DELETE #delete键

Guess you like

Origin blog.csdn.net/dcm1324659876/article/details/132366545