When selenium Webdriver is used for automated testing, some mouse and keyboard behaviors are often simulated. For example, you can use the mouse to click, double-click, right-click, or drag a keyboard. Alternatively, you can enter a keyboard, use a shortcut key, or use a combination of keys to simulate keyboard operations. In webderiver, there is a dedicated class to implement these test scenarios, that is, the actionchains class, which will be used together with the keys Data Storage Class in the process of using this class for keyboard operations, keys includes all the special keys on the keyboard.
1. Click the mouse
Click (element = none) left-click context_click (element = none) Right-click double_click (element = none) double-click move_to_element (element) and move the mouse to the middle of the element (hover) drag_and_drop (source, target) source, click the left button and drag it to the target element. click_and_hold (element = none) Click the left mouse button on the element to release the mouse perform () and execute the action stored in actionchains.
If element has the default value of none, it means that the parameter is not input. This action is executed in the same place.
The mouse event usage example is as follows:
Example 1: Left click
Action = actionchains (driver) action. click () # click action at the current mouse position. perform () # execute the action stored by action. click (driver. find_element_by_link_text ('News ')). perform () # click The 'News' element # Note: action. the click () action is not executed. It is only stored in the Action instance and needs to be passed through action. the perform () method executes the save # Save action. Multiple mouse and keyboard event actions can be stored and then executed at one time. Run cytl + C: actionchains (driver). key_down (Keys. Control). send_keys ('C'). key_up (Keys. Control). Perform ()
Example 2: Right-click
Action = actionchains (driver) action. context_click (). perform () # Right-click action at the current mouse position. context_click (driver. find_element_by_link_text ('News ')). perform () # Right-click the 'News' Element
Example 3: Double-click
Action = actionchains (driver) action. double_click (). perform () # double-click action at the current mouse position. double_click (driver. find_element_by_link_text ('News ')). perform () # double-click the 'News' element.
Example 4: move the mouse
# Move the cursor to the central point Action on The 'News' element. move_to_element (element ). perform () # move the cursor from the original position to the X axis X offset and Y axis yoffset; xoffset and yoffset can be positive and negative values. move_by_offset (-200,100 ). perform () # move the cursor to the center point of the element. The offset is xoffset and yoffsetaction. move_to_element_with_offset (element,-500,600 ). perform () action. move_by_offset (xoffset, yoffset) # Note that if xoffset is a negative number, the x-axis moves to the left, and the yoffset is a negative number to move the x-axis up. If the value # is greater than the current screen size, the mouse can only move to the most boundary position of the screen. In the test environment, the mouse movement operation is commonly used to obtain the flyover/tips of an element. In actual applications, many flyovers only appear after the mouse moves to this element, therefore, by executing the move_to_element (to_element) operation, the expected results can be achieved. Based on my personal experience, this method does not work for the flyover/tips of icons of certain products. Although flyover can appear when you move the mouse over these icons during manual operations, however, when the Webdriver is used to simulate this operation, although the method is successfully executed, flyover does not come out. Therefore, in actual application, you also need to handle specific product pages.
Example 5: hover the mouse over
Action. click_and_hold (). perform () # do not release the action when you press the mouse at the current position. click_and_hold (driver. find_element_by_link_text ('set ')). perform () # hover the cursor over 'settings' for action. the click_and_hold (element) method actually performs two actions. First, move the cursor to the element and then click_and_hold, so this method can also be written as: action. move_to_element (element ). click_and_hold ()
Example 6: drag the mouse
Target = driver. find_element_by_id ("SK") # obtain the target element # Drag the element source to the target location actionchains (driver ). drag_and_drop (source, target ). perform () # Drag and Drop the mouse to move the source element to the X and Y axes (xoffset and yoffset). xoffset is the abscissa, and yoffset is the ordinate. Actionchains (driver). drag_and_drop_by_offset (source,-100,100). Perform ()
The combined action of the mouse has been used during the drag and drop process. First, click and hold the lick_and_hold (source) element, and then execute the mouse movement (move_to ), move to the position of the target element or (xoffset, yoffset), and then execute the release action (release) of the mouse ). Therefore, the preceding method can be split into the following execution actions:
ActionChains(driver).click_and_hold(source).move_to_element(target).release().perform()
Example 7: Release the mouse
Action = actionchains (driver) action. Release (). Perform () # Release the mouse
2. keyboard operations
For keyboard simulation operations, the actionchains class provides methods such as pressing key_down (KEYS), releasing key_up (KEYS), and pressing and releasing send_keys (keys_to_send. There are two types of keyboard operations: normal keyboard and modify keyboard. The common keyboard is commonly used letters and numbers, the keyboard is modified to Ctrl, shift, ALT, and so on, and the keyboard is used in combination with other keys. When using the keyboard, You need to introduce from
Selenium. WebDriver. Common. Keys import keys package, which contains all special keys.
1. Normal keyboard operation
Use send_keys (Keys_to_send) method. This method supports the continuous operation of multiple keys. If you want to perform the key operation on an element, use send_keys_to_element (element,Keys_to_send) method. The following is an example:
From selenium. webDriver. common. keys import keysaction = actionchains (driver) action. send_keys (keys. space ). perform () # Press and release the Space key action. send_keys (keys. tab ). perform () # Press and release the tab key action. send_keys (keys. backspace ). perform () # Press and release the backspace key action. send_keys (keys. backspace, keys. space ). perform () # continuously execute the button action. send_keys (keys. tab ). send_keys (keys. tab ). perform () # You can also combine ''' to send a keyboard button operation for an element, or input the '''element = driver. find_element_by_id ('query') # Use the keyboard to operate action on an element. send_keys_to_element (element, 'selenium '). perform () # The preceding action is split into the following action. click (element ). send_keys ('selenium '). perform ()
Note:
Apart from the send_keys (keys_to_send) method of the actionchains class, the webelement class also has a send_keys_to_element (keys_to_send) method. These two methods are basically the same for general input operations, with the following differences:
First: send_keys (* keys_to_send) in actions will not be released after the modifier key operation, that is, when actions is called. send_keys (keys. ALT), actions. send_keys (keys. control), action. send_keys (keys. shift), it is equivalent to calling actions. key_down (keys_to_send), and if you want to simulate the press and release these modifiers in real applications, you should first take action. reset_actions () reset action and then call action. send_keys (keys. null ). perform () cancels the modifier key.
Second, in Webdriver, we can use send_keys () of the webelement class to upload attachments, such as element. send_keys ("D: \ test \ uploadfile \ test.jpg") file, but you cannot use actionchains to upload attachments, because the input box of type = 'file' does not support keyboard input.
2. Use of modifier keys
A modifier is one or a group of special keys on the keyboard. It is used together with a general key to temporarily change the normal behavior of a general keyboard.
Modifier keys are generally used in combination with common keys, such as Ctrl + A and ALT + F4.
Modification keys in our computer generally include Ctrl, ALT (option), shift, altgr, Windows logo, command, FN (function ). Generally, the first three methods are used.
For modification keys, in Python selenium, you can use the combination of pressing key_down (KEYS), releasing key_up (KEYS), and pressing and releasing send_keys (keys_to_send.
Action = actionchains (driver) action. key_down (keys. control ). perform () # press the ctrl key action. key_up (keys. control ). perform () # Release the ctrl key action. key_down (keys. shift ). perform () # press the Shift key action. key_up (keys. shift ). perform () # Release the Shift key action. key_down (keys. ALT ). perform () # press the Alt key action. key_up (keys. ALT ). perform () # Release the Alt key
Example: use Ctrl + C to copy text
ActionChains(driver).key_down(Keys.CONTROL).send_keys(‘c‘).key_up(Keys.CONTROL).perform()
3. webelement. send_keys () keyboard operation
Send_keys under the webelement object also supports the combination of keyboard operations.
The sample code is as follows:
Element = driver. find_element_by_id ('query') element. send_keys ('selenium ') element. send_keys (keys. back_space) # delete a character element by backspace. send_keys (keys. space) # space element. send_keys (keys. control, 'A') # select all (CTRL + a) elements. send_keys (keys. control, 'C') # copy (CTRL + C) element. send_keys (keys. control, 'V') # paste (CTRL + V) element. send_keys (keys. tab) # tabulation key (Tab) element. send_keys (keys. escape) # Return key (ESC) element. send_keys (keys. enter) # Enter)
If you are interested in testing technology, join QQ Group 706315665 to learn and discuss with each other.
Some friends in the group have sorted out their knowledge systems (source code, notes, PPT, and learning videos). You are welcome to add them to the Group for free.
Add QQ Group 706315665 to receive free materials
Complete UI mouse and keyboard events!