Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Selenium Mouse and Keyboard Operations

Selenium Mouse and Keyboard Operations

Selenium provides rich mouse and keyboard operation features that can simulate user interactions, such as clicking, double-clicking, dragging and dropping, hovering, entering text, and pressing key combinations.

The following are common mouse and keyboard operations in Selenium and their descriptions:

Mouse operations

Operation types Method Description
Click click(element) or click() Click the specified element or the current mouse position.
Double-click double_click(element) or double_click() Double-click the specified element or the current mouse position.
Right Click context_click(element) or context_click() Right-click the specified element or the current mouse position.
Drag and Drop drag_and_drop(source, target) Drag the source element onto the target element.
Hover move_to_element(element) Move the mouse over the specified element.
Press and Release click_and_hold(element) and release() Press and hold the specified element then release, used to implement drag operations.

Keyboard operations

Operation types Method Description
Enter text send_keys("text") Enter the specified text into an input box or text area.
Press Key Combination send_keys(Keys.CONTROL + 'a') Simulate pressing a key combination (such as Ctrl + A).
Press a Single Key send_keys(Keys.KEY_NAME) Simulate pressing a single key (such as Enter, Tab, Shift, etc.).
Release Key key_up(key) Release the pressed key.

Common Keyboard Key Values

values Description
Keys.ENTER Enter Key
Keys.TAB Tab Key
Keys.SHIFT Shift Key
Keys.CONTROL Ctrl Key
Keys.ALT Alt Key
Keys.ESCAPE Esc Key
Keys.BACKSPACE Backspace
Keys.SPACE Space Key
Keys.ARROW_UP Up Arrow Key
Keys.ARROW_DOWN Down Arrow Key
Keys.ARROW_LEFT Left Arrow Key
Keys.ARROW_RIGHT Right Arrow Key

Mouse Operations (ActionChains Class)

In Selenium, mouse operations are mainly performed through ActionChains class.ActionChains class allows us to perform complex mouse operations, such as clicking, double-clicking, right-clicking, dragging and dropping, and hovering.

Click, Double Click, Right Click

  1. Click: Simulates a left mouse button click.
  2. Double Click: Simulates a left mouse button double-click.
  3. Right Click: Simulates a right mouse button click.

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

action_chains

import

ActionChains

from

selenium.

webdriver

.

common

.

by

import

By

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://example.com"

)

Locate the element

element

=

driver.

find_element

(

By.

ID

,

"element_id"

)

Create an ActionChains Object

actions

=

ActionChains

(

driver

)

Click Operation

actions.

click

(

element

)

.

perform

(

)

Double-Click Operation

actions.

double_click

(

element

)

.

perform

(

)

Right-Click Operation

actions.

context_click

(

element

)

.

perform

(

)

Close the browser

driver.

quit

(

)

Drag-and-Drop Operation

Drag-and-drop refers to dragging one element to another position or onto another element.

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

action_chains

import

ActionChains

from

selenium.

webdriver

.

common

.

by

import

By

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://example.com"

)

Locate the Dragged Element and the Target Element

source_element

=

driver.

find_element

(

By.

ID

,

"source_element_id"

)

target_element

=

driver.

find_element

(

By.

ID

,

"target_element_id"

)

Create an ActionChains Object

actions

=

ActionChains

(

driver

)

Drag-and-Drop Operation

actions.

drag_and_drop

(

source_element

,

target_element

)

.

perform

(

)

Close the browser

driver.

quit

(

)

Hover Operation

Hover operation refers to placing the mouse pointer over an element, usually to trigger a dropdown menu or display a tooltip.

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

action_chains

import

ActionChains

from

selenium.

webdriver

.

common

.

by

import

By

Initialize the Browser Driver

driver

=

webdriver.

Chrome

(

)

Open a web page

driver.

get

(

"https://example.com"

)

Locate the element

element

=

driver.

find_element

(

By.

ID

,

"element_id"

)

Create an ActionChains Object

actions

=

ActionChains

(

driver

)

Hover Operation

actions.

move_to_element

(

element

)

.

perform

(

)

Close the browser

driver.

quit

(

)

Keyboard Operations (Keys Class)

Keyboard operations in Selenium are mainly performed through Keys class.Keys class provides simulation of various keyboard keys, including sending key combinations and special key operations.

Send Key Combinations

Key combination operations mean pressing multiple keys simultaneously, for example Ctrl + C(Copy) or Ctrl + V(Paste).

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

keys

import

Keys

from

selenium.

webdriver

.

common

.

by

import

By

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://example.com"

)

Locate the Input Box

input_element

=

driver.

find_element

(

By.

ID

,

"input_element_id"

)

Enter Content and Send Key Combination

input_element.

send_keys

(

"Hello, World!"

)

input_element.

send_keys

(

Keys.

CONTROL

,

'a'

)

 

Select All

input_element.

send_keys

(

Keys.

CONTROL

,

'c'

)

 

Copy

input_element.

send_keys

(

Keys.

CONTROL

,

'v'

)

 

Paste

Close the browser

driver.

quit

(

)

Special Key Operations (Enter, Tab, Shift, etc.)

Special key operations refer to simulating pressing special keys on the keyboard, such as Enter, Tab, Shift , etc.

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

keys

import

Keys

from

selenium.

webdriver

.

common

.

by

import

By

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://example.com"

)

Locate the Input Box

input_element

=

driver.

find_element

(

By.

ID

,

"input_element_id"

)

Enter Content and Press the Enter Key

input_element.

send_keys

(

"Hello, World!"

)

input_element.

send_keys

(

Keys.

ENTER

)

Press the Tab Key to Switch to the Next Element

input_element.

send_keys

(

Keys.

TAB

)

Press the Shift Key and Enter an Uppercase Letter

input_element.

send_keys

(

Keys.

SHIFT

,

"a"

)

 

Enter the uppercase 'A'

Close the browser

driver.

quit

(

)


Comprehensive Example

Here is a complete example showing how to use mouse and keyboard operations:

Example

from

selenium

import

webdriver

from

selenium.

webdriver

.

chrome

.

service

import

Service

as

ChromeService

from

selenium.

webdriver

.

common

.

by

import

By

from

selenium.

webdriver

.

common

.

action_chains

import

ActionChains

from

selenium.

webdriver

.

common

.

keys

import

Keys

Set the correct driver path

service

=

ChromeService

(

executable_path

=

"./chromedriver-mac-arm64/chromedriver"

)

options

=

webdriver.

ChromeOptions

(

)

driver

=

webdriver.

Chrome

(

service

=

service

,

options

=

options

)

Open a web page

driver.

get

(

"https://www.example.com"

)

Mouse operations

element

=

driver.

find_element

(

By.

ID

,

"button"

)

actions

=

ActionChains

(

driver

)

actions.

move_to_element

(

element

)

.

click

(

)

.

perform

(

)

Keyboard operations

input_box

=

driver.

find_element

(

By.

ID

,

"input-box"

)

input_box.

send_keys

(

"Hello, Selenium!"

)

input_box.

send_keys

(

Keys.

ENTER

)

Close the browser

driver.

quit

(

)