October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Perform Mouse Actions in Selenium WebDriver

Use Selenium’s Actions API to click, hover, right-click, double-click, and drag elements, with Java and Python examples plus viewport and input-state guidance.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Selenium’s Actions API for mouse gestures: build a chain such as a click, hover, right-click, or drag-and-drop, then call perform() to send it to the browser. In Java, the common pattern is new Actions(driver).click(element).perform(); in Python, use ActionChains(driver).click(element).perform(). The exact method signatures vary by language binding, so use the examples for your chosen language and check its current Selenium reference.

How Selenium mouse actions work

The Selenium Project describes the Actions API as “a low-level interface for providing virtualized device input actions to the web browser.” Its input sources are key, pointer, and wheel; mouse gestures use the pointer source. The API provides convenience methods for common gestures and lower-level commands for cases that need more control. Selenium Actions API documentation

Most mouse interactions follow the same pattern: locate the target, add one or more actions to a chain, and call perform(). A chain can include multiple steps, with a pause between them if the page needs time to respond. For low-level sequences involving multiple input devices, the caller is responsible for synchronizing their actions.

Common mouse actions in Java

These examples use Selenium’s Java Actions class. They assume driver is an initialized WebDriver and the target element has been located. Import org.openqa.selenium.interactions.Actions and org.openqa.selenium.WebElement as needed. The methods shown are documented in the Java API reference; consult the version used by your project if a signature differs. Selenium Java Actions API

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Click

Clicking an element targets its center. Use this element-based form when the element itself identifies the intended click target:

new Actions(driver).click(element).perform();

To click at the pointer’s current position instead, call click() without an element.

Click and hold

Move to the element and press the left mouse button without releasing it. This can be useful when the page requires a held press or as the start of a drag:

new Actions(driver).clickAndHold(element).perform();

Right-click

Selenium names a right-click a context click:

new Actions(driver).contextClick(element).perform();

Double-click

Move to the element and press and release the left button twice:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
new Actions(driver).doubleClick(element).perform();

Hover

Move the pointer to the element’s in-view center:

new Actions(driver).moveToElement(element).perform();

The element must be in the viewport for this command; otherwise the mouse action can error.

Drag and drop

For an element-to-element drag, Selenium’s helper presses and holds at the source, moves to the target, and releases:

new Actions(driver).dragAndDrop(source, target).perform();

To drag by an offset rather than to another element, use the related offset helper:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
new Actions(driver).dragAndDropBy(source, 30, -10).perform();

Common mouse actions in Python

Python uses ActionChains rather than Java’s Actions. The examples assume driver is an initialized WebDriver and element, source, and target are located elements. Import the class with from selenium.webdriver.common.action_chains import ActionChains. Method spelling and parameter conventions vary by binding and release; the Python API reference documents the corresponding methods. Selenium Python ActionChains API

Click, hold, right-click, and double-click

ActionChains(driver).click(element).perform()
ActionChains(driver).click_and_hold(element).perform()
ActionChains(driver).context_click(element).perform()
ActionChains(driver).double_click(element).perform()

As in Java, calling click() without an element clicks at the current pointer position.

Hover and drag-and-drop

ActionChains(driver).move_to_element(element).perform()
ActionChains(driver).drag_and_drop(source, target).perform()
ActionChains(driver).drag_and_drop_by_offset(source, 30, -10).perform()

Choose an element target or an offset

Use an element-based method when Selenium can locate the page element you need to interact with. Use a coordinate offset when the interaction depends on a particular point rather than the element as a whole.

Selenium documents offsets relative to an element, the viewport, or the current pointer position. Positive X moves right; positive Y moves down. For example, an offset of (30, -10) from the current pointer location moves 30 pixels right and 10 pixels up. Keep the pointer within the viewport: a coordinate move that takes it outside the viewport can fail.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Build and execute a multi-step gesture

  1. Locate the element or coordinate relevant to the interaction and make sure it is available.
  2. Choose a convenience method for the gesture; use offsets only when the interaction requires a specific point.
  3. Chain the related actions. Add a pause only if the page needs time between steps.
  4. Call perform() to execute the composed sequence.
  5. If the sequence leaves a mouse button or modifier held, clear or reset the action input state using the mechanism available in your binding and driver.

For a complex gesture that needs explicit control over pointer movement, button presses, or timing, use the lower-level Actions API rather than a single convenience method. When combining low-level sequences from multiple input devices, keep their timing synchronized yourself.

Troubleshoot mouse actions

  • Hover fails because the element is outside the viewport: Selenium’s hover action targets the in-view center. Make the element available in the viewport before calling moveToElement or its binding equivalent.
  • An offset move fails: Check the pointer’s starting location and the sign and size of each offset. Positive X is right and positive Y is down; keep the destination inside the viewport.
  • A drag does not release as expected: A drag consists of pressing and holding, moving, then releasing. If a sequence ends with a button held, clear or reset input state with the binding’s supported mechanism.
  • A method name or signature is not recognized: Selenium bindings use different method names and parameter conventions. Check the API reference for your language and installed Selenium release instead of copying a method spelling from another binding.
  • A multi-device sequence behaves out of order: With low-level actions, the caller must synchronize input-device sequences. Review the order and timing of the composed actions.

Or skip the browser setup

If you need a screenshot rather than interactive browser control, ScreenshotNeo is a website screenshot API and MCP server. One GET request can return a PNG, JPEG, WebP, or PDF; its consent-banner and popup cleanup is separate from Selenium mouse automation.

cURL example, using the documented endpoint and parameter format (see the ScreenshotNeo documentation):

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, with the outcome reported in response headers. Its MCP server gives AI agents tools for screenshots, page information, and PDF capture. The free plan includes 1,000 shots per month without a card; paid plans start at $5 for 3,000 shots.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month with no card.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.