Skip to main content
SeleniumDecoded

MoveTargetOutOfBoundsException

MoveTargetOutOfBoundsException: Causes and Fixes

Fix Selenium MoveTargetOutOfBoundsException (MoveTargetOutOfBoundsError in JS): Actions moves outside the viewport, centre offsets and small headless windows.

Occasional W3C: move target out of bounds Verified against Selenium 4.52.0
Exception name by language
Java org.openqa.selenium.interactions.MoveTargetOutOfBoundsException
Python selenium.common.exceptions.MoveTargetOutOfBoundsException
JavaScript MoveTargetOutOfBoundsError
C# OpenQA.Selenium.MoveTargetOutOfBoundsException

What the error means

An Actions API pointer move tried to put the mouse somewhere outside the browser’s viewport. W3C actions work in viewport coordinates, and the pointer cannot leave the visible area of the page. If the target point (an element’s centre plus any offset, or the current position plus an offset) falls outside it, the driver throws “move target out of bounds”.

Unlike element.click(), the Actions API does not scroll automatically in every browser. click() scrolls the element into view first; moveToElement relies on the element already being in the viewport in Firefox, and an offset can push the point off-screen in any browser.

What it looks like

// Java
org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: move target out of bounds
(Session info: chrome=131.0.6778.86)
at org.openqa.selenium.remote.RemoteWebDriver.perform(RemoteWebDriver.java:...)
# Python (Firefox)
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: Move target (640, 1430) is out of bounds of viewport dimensions (1280, 720)
// JavaScript (selenium-webdriver)
MoveTargetOutOfBoundsError: move target out of bounds
(Session info: chrome=131.0.6778.86)
// C#
OpenQA.Selenium.MoveTargetOutOfBoundsException : move target out of bounds

Firefox’s message is the most useful because it prints the computed target and the viewport size. If you are debugging in Chrome, temporarily run the same test in Firefox to get those numbers.

Common causes

  1. The element is below the fold. You call moveToElement (hover, drag source, drag target) on an element that has not been scrolled into view.
  2. Offsets are measured from the centre, not the top-left. In Selenium 4, moveToElement(el, x, y) offsets from the element’s centre. Code written for Selenium 3, which used the top-left corner, now overshoots.
  3. Chained moveByOffset calls accumulate. Each move is relative to the current pointer position, so a loop of moves drifts off the page.
  4. The viewport is smaller than you think. Headless browsers start at a small default size, so coordinates that work on your monitor fall outside the CI viewport.
  5. Drag targets in scrollable containers. The drop target is inside an overflowing panel and is outside the visible part of that panel.

How to fix it

The element is below the fold

Scroll the element into view as part of the action chain. Selenium 4.2+ has a wheel action for this in all four bindings.

Scroll to the element, then hover
Selenium 4 Stable
WebElement menu = driver.findElement(By.id("account-menu"));
new Actions(driver)
.scrollToElement(menu)
.moveToElement(menu)
.perform();
from selenium.webdriver.common.action_chains import ActionChains
menu = driver.find_element(By.ID, "account-menu")
ActionChains(driver).scroll_to_element(menu).move_to_element(menu).perform()
const menu = await driver.findElement(By.id('account-menu'));
await driver.actions()
.scroll(0, 0, 0, 0, menu)
.move({ origin: menu })
.perform();
IWebElement menu = driver.FindElement(By.Id("account-menu"));
new Actions(driver)
.ScrollToElement(menu)
.MoveToElement(menu)
.Perform();

scrollToElement scrolls the minimum amount needed, which can leave the element at the very bottom edge, under a sticky footer. If that happens, centre it with scrollIntoView({block: 'center'}) via executeScript before the action. See the scrolling lesson.

Offsets are measured from the centre

Convert top-left offsets to centre-relative ones. To click 10 pixels from the left edge of an element:

Centre-relative offsets in Selenium 4
Selenium 4 Stable
WebElement slider = driver.findElement(By.id("price-slider"));
Rectangle r = slider.getRect();
int xFromCentre = 10 - r.getWidth() / 2; // 10px from the left edge
new Actions(driver)
.moveToElement(slider, xFromCentre, 0)
.click()
.perform();
slider = driver.find_element(By.ID, "price-slider")
rect = slider.rect
x_from_centre = 10 - rect["width"] // 2 # 10px from the left edge
ActionChains(driver).move_to_element_with_offset(slider, x_from_centre, 0).click().perform()
const slider = await driver.findElement(By.id('price-slider'));
const rect = await slider.getRect();
const xFromCentre = 10 - Math.floor(rect.width / 2); // 10px from the left edge
await driver.actions()
.move({ origin: slider, x: xFromCentre, y: 0 })
.click()
.perform();
IWebElement slider = driver.FindElement(By.Id("price-slider"));
int xFromCentre = 10 - slider.Size.Width / 2; // 10px from the left edge
new Actions(driver)
.MoveToElement(slider, xFromCentre, 0)
.Click()
.Perform();

Keep every offset within half the element’s width and height and the point stays on the element.

Chained moveByOffset calls accumulate

Reset to a known origin for each step instead of chaining relative moves: move to the element (or to the viewport origin) and then apply a single offset. When dragging a slider in increments, compute each target from the element, not from the previous pointer position.

The viewport is smaller than you think

Set the window size explicitly so headless and headed runs match.

Fixed window size for predictable coordinates
Selenium 4 Stable
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new", "--window-size=1920,1080");
WebDriver driver = new ChromeDriver(options);
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(options=options)
const chrome = require('selenium-webdriver/chrome');
const options = new chrome.Options().addArguments('--headless=new', '--window-size=1920,1080');
const driver = await new Builder().forBrowser('chrome').setChromeOptions(options).build();
var options = new ChromeOptions();
options.AddArguments("--headless=new", "--window-size=1920,1080");
var driver = new ChromeDriver(options);

You can confirm the real viewport with executeScript("return [window.innerWidth, window.innerHeight];").

Drag targets in scrollable containers

Scroll the container so the drop target is visible before starting the drag, either with scrollToElement on the target or by setting the container’s scrollTop via script. For HTML5 drag-and-drop that does not respond to Actions at all, see the drag and drop lesson.

How to prevent it

  • Always include a scroll step before hover and drag actions on elements that may be off-screen.
  • Remember Selenium 4 offsets are centre-based; review migrated Selenium 3 code for moveToElement(el, x, y).
  • Pin the window size in your driver factory.
  • Prefer element-origin moves over viewport coordinates; they survive layout changes.
  • Prefer plain click() over an Actions click when you do not need a hover or offset; it scrolls for you.

Go deeper