Mouse actions
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:
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”:
Alternate Button Clicks
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”:
Back Click
There is no convenience method for this, it is just pressing and releasing mouse button 3
Forward Click
There is no convenience method for this, it is just pressing and releasing mouse button 4
Double click
This method combines moving to the center of an element with pressing and releasing the left mouse button twice.
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.
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.
Offset from Viewport
This method moves the mouse from the upper left corner of the current viewport by the provided offset.
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.
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.
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.