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:
WebElement clickable = driver.findElement(By.id("clickable"));
new Actions(driver)
.clickAndHold(clickable)
.perform();
.click_and_hold(clickable)\
.perform()
sleep(0.5)
IWebElement clickable = driver.FindElement(By.Id("clickable"));
new Actions(driver)
.ClickAndHold(clickable)
.Perform();
clickable = driver.find_element(id: 'clickable')
driver.action
.click_and_hold(clickable)
.perform
it('Mouse move and mouseDown on an element', async function () {
await driver.get('https://www.selenium.dev/selenium/web/mouse_interaction.html');
let clickable = driver.findElement(By.id("clickable"));
val clickable = driver.findElement(By.id("clickable"))
Actions(driver)
.clickAndHold(clickable)
.perform()
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”:
WebElement clickable = driver.findElement(By.id("click"));
new Actions(driver)
.click(clickable)
.perform();
.click(clickable)\
.perform()
assert "resultPage.html" in driver.current_url
IWebElement clickable = driver.FindElement(By.Id("click"));
new Actions(driver)
.Click(clickable)
.Perform();
clickable = driver.find_element(id: 'click')
driver.action
.click(clickable)
.perform
it('Mouse move and click on an element', async function () {
await driver.get('https://www.selenium.dev/selenium/web/mouse_interaction.html');
let click = driver.findElement(By.id("click"));
val clickable = driver.findElement(By.id("click"))
Actions(driver)
.click(clickable)
.perform()
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”:
WebElement clickable = driver.findElement(By.id("clickable"));
new Actions(driver)
.contextClick(clickable)
.perform();
.context_click(clickable)\
.perform()
sleep(0.5)
IWebElement clickable = driver.FindElement(By.Id("clickable"));
new Actions(driver)
.ContextClick(clickable)
.Perform();
clickable = driver.find_element(id: 'clickable')
driver.action
.context_click(clickable)
.perform
it('Mouse move and right click on an element', async function () {
await driver.get('https://www.selenium.dev/selenium/web/mouse_interaction.html');
const clickable = driver.findElement(By.id("clickable"));
val clickable = driver.findElement(By.id("clickable"))
Actions(driver)
.contextClick(clickable)
.perform()
Back Click
There is no convenience method for this, it is just pressing and releasing mouse button 3
PointerInput mouse = new PointerInput(PointerInput.Kind.MOUSE, "default mouse");
Sequence actions = new Sequence(mouse, 0)
.addAction(mouse.createPointerDown(PointerInput.MouseButton.BACK.asArg()))
.addAction(mouse.createPointerUp(PointerInput.MouseButton.BACK.asArg()));
((RemoteWebDriver) driver).perform(Collections.singletonList(actions));
action.pointer_action.pointer_up(MouseButton.BACK)
action.perform()
assert driver.title == "BasicMouseInterfaceTest"
ActionBuilder actionBuilder = new ActionBuilder();
PointerInputDevice mouse = new PointerInputDevice(PointerKind.Mouse, "default mouse");
actionBuilder.AddAction(mouse.CreatePointerDown(MouseButton.Back));
actionBuilder.AddAction(mouse.CreatePointerUp(MouseButton.Back));
((IActionExecutor)driver).PerformActions(actionBuilder.ToActionSequenceList());
driver.action
.pointer_down(:back)
.pointer_up(:back)
.perform
assert.deepStrictEqual(await driver.getTitle(), `We Arrive Here`)
val mouse = PointerInput(PointerInput.Kind.MOUSE, "default mouse")
val actions = Sequence(mouse, 0)
.addAction(mouse.createPointerDown(PointerInput.MouseButton.BACK.asArg()))
.addAction(mouse.createPointerUp(PointerInput.MouseButton.BACK.asArg()))
(driver as RemoteWebDriver).perform(Collections.singletonList(actions))
Forward Click
There is no convenience method for this, it is just pressing and releasing mouse button 4
PointerInput mouse = new PointerInput(PointerInput.Kind.MOUSE, "default mouse");
Sequence actions = new Sequence(mouse, 0)
.addAction(mouse.createPointerDown(PointerInput.MouseButton.FORWARD.asArg()))
.addAction(mouse.createPointerUp(PointerInput.MouseButton.FORWARD.asArg()));
((RemoteWebDriver) driver).perform(Collections.singletonList(actions));
action.pointer_action.pointer_up(MouseButton.FORWARD)
action.perform()
assert driver.title == "We Arrive Here"
ActionBuilder actionBuilder = new ActionBuilder();
PointerInputDevice mouse = new PointerInputDevice(PointerKind.Mouse, "default mouse");
actionBuilder.AddAction(mouse.CreatePointerDown(MouseButton.Forward));
actionBuilder.AddAction(mouse.CreatePointerUp(MouseButton.Forward));
((IActionExecutor)driver).PerformActions(actionBuilder.ToActionSequenceList());
driver.action
.pointer_down(:forward)
.pointer_up(:forward)
.perform
assert.deepStrictEqual(await driver.getTitle(), `BasicMouseInterfaceTest`)
val mouse = PointerInput(PointerInput.Kind.MOUSE, "default mouse")
val actions = Sequence(mouse, 0)
.addAction(mouse.createPointerDown(PointerInput.MouseButton.FORWARD.asArg()))
.addAction(mouse.createPointerUp(PointerInput.MouseButton.FORWARD.asArg()))
(driver as RemoteWebDriver).perform(Collections.singletonList(actions))
Double click
This method combines moving to the center of an element with pressing and releasing the left mouse button twice.
WebElement clickable = driver.findElement(By.id("clickable"));
new Actions(driver)
.doubleClick(clickable)
.perform();
.double_click(clickable)\
.perform()
assert driver.find_element(By.ID, "click-status").text == "double-clicked"
IWebElement clickable = driver.FindElement(By.Id("clickable"));
new Actions(driver)
.DoubleClick(clickable)
.Perform();
clickable = driver.find_element(id: 'clickable')
driver.action
.double_click(clickable)
.perform
it('Double-click on an element', async function () {
await driver.get('https://www.selenium.dev/selenium/web/mouse_interaction.html');
const clickable = driver.findElement(By.id("clickable"));
val clickable = driver.findElement(By.id("clickable"))
Actions(driver)
.doubleClick(clickable)
.perform()
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.
WebElement hoverable = driver.findElement(By.id("hover"));
new Actions(driver)
.moveToElement(hoverable)
.perform();
.move_to_element(hoverable)\
.perform()
assert driver.find_element(By.ID, "move-status").text == "hovered"
IWebElement hoverable = driver.FindElement(By.Id("hover"));
new Actions(driver)
.MoveToElement(hoverable)
.Perform();
hoverable = driver.find_element(id: 'hover')
driver.action
.move_to(hoverable)
.perform
it('Mouse move into an element', async function () {
await driver.get('https://www.selenium.dev/selenium/web/mouse_interaction.html');
const hoverable = driver.findElement(By.id("hover"));
val hoverable = driver.findElement(By.id("hover"))
Actions(driver)
.moveToElement(hoverable)
.perform()
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.
WebElement tracker = driver.findElement(By.id("mouse-tracker"));
new Actions(driver)
.moveToElement(tracker, 8, 0)
.perform();
.move_to_element_with_offset(mouse_tracker, 8, 0)\
.perform()
coordinates = driver.find_element(By.ID, "relative-location").text.split(", ")
IWebElement tracker = driver.FindElement(By.Id("mouse-tracker"));
new Actions(driver)
.MoveToElement(tracker, 8, 0)
.Perform();
mouse_tracker = driver.find_element(id: 'mouse-tracker')
driver.action
.move_to(mouse_tracker, 8, 11)
.perform
it('From element', async function () {
await driver.get('https://www.selenium.dev/selenium/web/mouse_interaction.html');
const mouseTracker = driver.findElement(By.id("mouse-tracker"));
val tracker = driver.findElement(By.id("mouse-tracker"))
Actions(driver)
.moveToElement(tracker, 8, 0)
.perform()
Offset from Viewport
This method moves the mouse from the upper left corner of the current viewport by the provided offset.
PointerInput mouse = new PointerInput(PointerInput.Kind.MOUSE, "default mouse");
Sequence actions = new Sequence(mouse, 0)
.addAction(mouse.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), 8, 12));
((RemoteWebDriver) driver).perform(Collections.singletonList(actions));
action.perform()
coordinates = driver.find_element(By.ID, "absolute-location").text.split(", ")
ActionBuilder actionBuilder = new ActionBuilder();
PointerInputDevice mouse = new PointerInputDevice(PointerKind.Mouse, "default mouse");
actionBuilder.AddAction(mouse.CreatePointerMove(CoordinateOrigin.Viewport,
8, 0, TimeSpan.Zero));
((IActionExecutor)driver).PerformActions(actionBuilder.ToActionSequenceList());
driver.action
.move_to_location(8, 12)
.perform
it('From viewport origin', async function () {
await driver.get('https://www.selenium.dev/selenium/web/mouse_interaction.html');
val mouse = PointerInput(PointerInput.Kind.MOUSE, "default mouse")
val actions = Sequence(mouse, 0)
.addAction(mouse.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), 8, 12))
(driver as RemoteWebDriver).perform(Collections.singletonList(actions))
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.
new Actions(driver)
.moveByOffset(13, 15)
.perform();
.perform()
coordinates = driver.find_element(By.ID, "absolute-location").text.split(", ")
new Actions(driver)
.MoveByOffset(13, 15)
.Perform();
driver.action
.move_by(13, 15)
.perform
await actions.move({x: 6, y: 3}).perform()
Actions(driver)
.moveByOffset(13, 15)
.perform()
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.
WebElement draggable = driver.findElement(By.id("draggable"));
WebElement droppable = driver.findElement(By.id("droppable"));
new Actions(driver)
.dragAndDrop(draggable, droppable)
.perform();
ActionChains(driver)\
.drag_and_drop(draggable, droppable)\
.perform()
assert driver.find_element(By.ID, "drop-status").text == "dropped"
IWebElement draggable = driver.FindElement(By.Id("draggable"));
IWebElement droppable = driver.FindElement(By.Id("droppable"));
new Actions(driver)
.DragAndDrop(draggable, droppable)
.Perform();
draggable = driver.find_element(id: 'draggable')
droppable = driver.find_element(id: 'droppable')
driver.action
.drag_and_drop(draggable, droppable)
.perform
it('Onto Element', async function () {
await driver.get('https://www.selenium.dev/selenium/web/mouse_interaction.html');
const draggable = driver.findElement(By.id("draggable"));
const droppable = await driver.findElement(By.id("droppable"));
val draggable = driver.findElement(By.id("draggable"))
val droppable = driver.findElement(By.id("droppable"))
Actions(driver)
.dragAndDrop(draggable, droppable)
.perform()
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.
WebElement draggable = driver.findElement(By.id("draggable"));
Rectangle start = draggable.getRect();
Rectangle finish = driver.findElement(By.id("droppable")).getRect();
new Actions(driver)
.dragAndDropBy(draggable, finish.getX() - start.getX(), finish.getY() - start.getY())
.perform();
finish = driver.find_element(By.ID, "droppable").location
ActionChains(driver)\
.drag_and_drop_by_offset(draggable, finish['x'] - start['x'], finish['y'] - start['y'])\
.perform()
assert driver.find_element(By.ID, "drop-status").text == "dropped"
IWebElement draggable = driver.FindElement(By.Id("draggable"));
Point start = draggable.Location;
Point finish = driver.FindElement(By.Id("droppable")).Location;
new Actions(driver)
.DragAndDropToOffset(draggable, finish.X - start.X, finish.Y - start.Y)
.Perform();
draggable = driver.find_element(id: 'draggable')
start = draggable.rect
finish = driver.find_element(id: 'droppable').rect
driver.action
.drag_and_drop_by(draggable, finish.x - start.x, finish.y - start.y)
.perform
it('By Offset', async function () {
await driver.get('https://www.selenium.dev/selenium/web/mouse_interaction.html');
const draggable = driver.findElement(By.id("draggable"));
let start = await draggable.getRect();
let finish = await driver.findElement(By.id("droppable")).getRect();
val draggable = driver.findElement(By.id("draggable"))
val start = draggable.getRect()
val finish = driver.findElement(By.id("droppable")).getRect()
Actions(driver)
.dragAndDropBy(draggable, finish.getX() - start.getX(), finish.getY() - start.getY())
.perform()