Backgroud:

In selenium automation, you will encounter situations that require simulating mouse and keyboard operations, such as clicking, double-clicking, right-clicking, dragging, copying, pasting, deleting, and so on.

First, mouse events

Selenium provides a class to handle mouse events, ActionChains

from selenium.webdriver import ActionChains
# selenium.webdriver.common.action_chains.ActionChains(driver)
Copy the code

This class basically meets all mouse manipulation needs.

1. Basic usage of ActionChains

When the ActionChains method is called, it does not execute immediately. Instead, all operations are placed in a queue in order. When the Perform () method is called, the queue times are executed in order.

In this case there are two ways to call:

  • Chain writing:
self.driver = webdriver.Chrome() self.driver.get('http://www.baidu.com') link_text = Self. Driver. Find_element_by_link_text (' news') ActionChains(driver).move_to_element(link_text).click(hidden_submenu).perform()Copy the code
  • Step by step:
self.driver = webdriver.Chrome() self.driver.get('http://www.baidu.com') link_text = Self.driver.find_element_by_link_text (' news ') Actions = ActionChains(self.driver) Actions.move_to_element (link_text) actions.click(link_text) actions.perform()Copy the code
2. ActionChains method list
# methods Methods described
1 click(on_element=None) Click the left mouse button
2 click_and_hold(on_element=None) Click the left mouse button, do not release
3 context_click(on_element=None) Right mouse click
4 double_click(on_element=None) Double click the left mouse button
5 drag_and_drop(source, target) Drag to an element and release
6 drag_and_drop_by_offset(source, xoffset, yoffset) Drag to some coordinates and release
7 key_down(value, element=None) Press a key on a keyboard
8 key_up(value, element=None) Release a key
9 move_by_offset(xoffset, yoffset) Moves the mouse pointer from the current position to a coordinate
10 move_to_element(to_element) Mouse over an element
11 move_to_element_with_offset(to_element, xoffset, yoffset) Move to the number of miles away from an element (the upper-left coordinate)
12 perform() Perform all actions in the chain
13 release(on_element=None) Release the left mouse button at an element position
14 send_keys(*keys_to_send) Sends a key to the element in current focus
15 send_keys_to_element(element, *keys_to_send) Sends a key to the specified element

Keyboard events

The operation of the simulated keyboard needs to enter the keyboard module first:

from selenium.webdriver.common.keys import Keys
# selenium.webdriver.common.keys.Keys
Copy the code

The Keys() class provides methods for almost all Keys on the keyboard. The send_keys() method can be used to simulate keyboard input, but it can also be used to enter keys on the keyboard, or even key combinations such as Ctrl+A, Ctrl+C, etc.

1. The common key

For a normal keyboard, use sendKeys(keysToSend), such as TAB, Backspace, etc.

Send_keys (keys.back_space) # Delete key (BackSpace) send_keys(keys.space) # SPACE send_keys(keys.tab) # TAB Send_keys (keys.escape) # Return key (Esc) Send_keys (keys.enter) # Return key (ENTER) Send_keys (keys.f1) # keyboard F1Copy the code
2. Modifier keys

Modifier keys are one or a group of special keys on the keyboard that, when used in conjunction with regular keys, temporarily change the normal behavior of the regular keyboard. Pressing the modifier alone generally does not trigger any keyboard events on its own. The following keys are available: Shift, Ctrl, Alt(Option), AltGr, Windows Logo, Command, and FN(Function).

ActionChains(self.driver).keyDown(Keys.CONTROL); // Press Ctrl + ActionChains(self.driver).keyDown(keys.shift); ActionChains(self.driver).keyDown(key.alt); // Press Alt (ActionChains(self.driver).keyUp(key.control); // Release Ctrl + ActionChains(self.driver).keyup (keys.shift); ActionChains(self.driver).keyUp(keys.alt); // Release Alt keyCopy the code
3. Common key combination operations:
Send_keys (Keys.COMMAND, 'a') # select all (COMMAND+ a) send_keys(Keys.COMMAND, 'c') # copy (COMMAND+ c) send_keys(Keys.COMMAND, 'x') # cut (COMMAND+X) send_keys(Keys.COMMAND, 'v') # paste (COMMAND+V) # key_down(Keys.COMMAND) indicates that the COMMAND key is pressed, send_keys('v') simulates the V key, So this is Command plus v, Key_down (Keys.COM - MAND) indicates that the Command key is released ActionChains(self.driver).key_down(Keys.COMMAND).send_keys('a').key_up(Keys.COMMAND).perform() ActionChains(self.driver).key_down(Keys.COMMAND).send_keys('x').key_up(Keys.COMMAND).perform() ActionChains(self.driver).key_down(Keys.COMMAND).send_keys('v').key_up(Keys.COMMAND).perform() # Alt+F4 to close the current active window ActionChains(self.driver).keyDown(Keys.ALT).keyDown(Keys.F4).keyUp(Keys.ALT).perform()Copy the code

Note: ActionChains(self.driver).keyDown(theKey) is called. This key will remain held until keyUp(theKey) or sendKeys(keys.null) is released

Example Code:

from selenium import webdriver from time import sleep # http://sahitest.com/demo/ from selenium.webdriver import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.remote.webelement import WebElement  class TestCase(object): def __init__(self): Self.driver = webdriver.chrome () self.driver.maximize_window() # def test_mouse(self): The self. The driver. The get double_click_btn = (' http://sahitest.com/demo/clicks.htm ') # mouse click self.driver.find_element_by_xpath('/html/body/form/input[2]') ActionChains(self.driver).double_click(double_click_bTN).perform() sleep(2) # click single_click_bTN = self.driver.find_element_by_xpath('/html/body/form/input[3]') ActionChains(self.driver).click(single_click_btn).perform() ## Self.driver.find_element_by_xpath ('/ HTML /body/form/input[3]').click() sleep(2) # right-click right_click_bTN = self.driver.find_element_by_xpath('/html/body/form/input[4]') ActionChains(self.driver).context_click(right_click_btn).perform() # def test_keyboard(self): Self.driver. get('http://www.baidu.com') kw = self.driver.find_element_by_id('kw') kw.send_keys("selenium") # Command +A kw.send_keys(Keys.COMMAND, 'A ') sleep(2) # clipboard command+X kw.send_keys(Keys.COMMAND,' A ') 'x') sleep(2) # command+V kw.send_keys(Keys.COM) 'v') sleep(2) # mouse over an element ActionChains(self.driver).move_to_element(self.driver. Find_element_by_link_text (' baidu_page_break_tag_ ')).perform() sleep(2) Self.driver. quit() # def test_keyboard2(self): self.driver.get('http://www.baidu.com') input = self.driver.find_element_by_id('kw') input.send_keys('python') # Send_keys ('a') mimics the A key, so when combined, it's command + a, Key_down (Keys.COM - MAND) indicates that the Command key is released ActionChains(self.driver).key_down(Keys.COMMAND).send_keys('a').key_up(Keys.COMMAND).perform() sleep(2) ActionChains(self.driver).key_down(Keys.COMMAND).send_keys('x').key_up(Keys.COMMAND).perform() sleep(2) self.driver.find_element_by_id('kw').click() ActionChains(self.driver).key_down(Keys.COMMAND).send_keys('v').key_up(Keys.COMMAND).perform() self.driver.find_element_by_id('su').click() sleep(2) self.driver.quit() if __name__ == '__main__': case = TestCase() # case.test_mouse() case.test_keyboard() case.test_keyboard2()Copy the code