Mouse actions A representation of any pointer device for interacting with a web page.
There are only 3 actions that can be accomplished with a mouse:
pressing down on a button, releasing a pressed button, and moving the mouse.
Selenium provides convenience methods that combine these actions in the most common ways.
Click and hold This method combines moving the mouse to the center of an element with pressing the left mouse button.
This is useful for focusing a specific element:
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
/examples/python/tests/actions_api/test_mouse.py
Copy
Close
/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
/examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
/examples/javascript/test/actionsApi/mouse/clickAndHold.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
Click and release This method combines moving to the center of an element with pressing and releasing the left mouse button.
This is otherwise known as “clicking”:
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
/examples/python/tests/actions_api/test_mouse.py
Copy
Close
/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
/examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
/examples/javascript/test/actionsApi/mouse/clickAndRelease.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
There are a total of 5 defined buttons for a Mouse:
0 — Left Button (the default) 1 — Middle Button (currently unsupported) 2 — Right Button 3 — X1 (Back) Button 4 — X2 (Forward) Button Context Click This method combines moving to the center of an element with pressing and releasing the right mouse button (button 2).
This is otherwise known as “right-clicking”:
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
/examples/python/tests/actions_api/test_mouse.py
Copy
Close
/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
/examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
/examples/javascript/test/actionsApi/mouse/rightClick.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
Back Click There is no convenience method for this, it is just pressing and releasing mouse button 3
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
Selenium v4.2
/examples/python/tests/actions_api/test_mouse.py
Copy
Close
Selenium v4.2
/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
Selenium v4.2
/examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
Selenium v4.5.0
/examples/javascript/test/actionsApi/mouse/backAndForwardClick.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
Forward Click There is no convenience method for this, it is just pressing and releasing mouse button 4
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
Selenium v4.2
/examples/python/tests/actions_api/test_mouse.py
Copy
Close
Selenium v4.2
/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
Selenium v4.2
/examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
Selenium v4.5.0
/examples/javascript/test/actionsApi/mouse/backAndForwardClick.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
Double click This method combines moving to the center of an element with pressing and releasing the left mouse button twice.
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
/examples/python/tests/actions_api/test_mouse.py
Copy
Close
/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
/examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
/examples/javascript/test/actionsApi/mouse/doubleClick.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
Move to element This method moves the mouse to the in-view center point of the element.
This is otherwise known as “hovering.”
Note that the element must be in the viewport or else the command will error.
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
/examples/python/tests/actions_api/test_mouse.py
Copy
Close
/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
/examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
/examples/javascript/test/actionsApi/mouse/moveToElement.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
Move by offset These methods first move the mouse to the designated origin and then
by the number of pixels in the provided offset.
Note that the position of the mouse must be in the viewport or else the command will error.
Offset from Element This method moves the mouse to the in-view center point of the element,
then moves by the provided offset.
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
/examples/python/tests/actions_api/test_mouse.py
Copy
Close
/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
/examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
/examples/javascript/test/actionsApi/mouse/moveByOffset.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
Offset from Viewport This method moves the mouse from the upper left corner of the current viewport by the provided
offset.
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
/examples/python/tests/actions_api/test_mouse.py
Copy
Close
/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
/examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
/examples/javascript/test/actionsApi/mouse/moveByOffset.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
Offset from Current Pointer Location This method moves the mouse from its current position by the offset provided by the user.
If the mouse has not previously been moved, the position will be in the upper left
corner of the viewport.
Note that the pointer position does not change when the page is scrolled.
Note that the first argument X specifies to move right when positive, while the second argument
Y specifies to move down when positive. So moveByOffset(30, -10) moves right 30 and up 10 from
the current mouse position.
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
/examples/python/tests/actions_api/test_mouse.py
Copy
Close
/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
/examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
/examples/javascript/test/actionsApi/mouse/moveByOffset.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
Drag and Drop on Element This method firstly performs a click-and-hold on the source element,
moves to the location of the target element and then releases the mouse.
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
/examples/python/tests/actions_api/test_mouse.py
Copy
Close
/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
/examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
/examples/javascript/test/actionsApi/mouse/dragAndDrop.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close
Drag and Drop by Offset This method firstly performs a click-and-hold on the source element, moves to the given offset and then releases the mouse.
Java
Python
CSharp
Ruby
JavaScript
Kotlin /examples/java/src/test/java/dev/selenium/actions_api/MouseTest.java
Copy
Close
/examples/python/tests/actions_api/test_mouse.py
Copy
Close
/examples/dotnet/SeleniumDocs/ActionsAPI/MouseTest.cs
Copy
Close
/examples/ruby/spec/actions_api/mouse_spec.rb
Copy
Close
/examples/javascript/test/actionsApi/mouse/dragAndDrop.spec.js
Copy
Close
/examples/kotlin/src/test/kotlin/dev/selenium/actions_api/MouseTest.kt
Copy
Close