Turn your manual testers into automation experts! Request a DemoStart testRigor Free

How to Resolve MoveTargetOutOfBoundsException in Selenium?

MoveTargetOutOfBoundsException is thrown when an action attempt is made to an element that is not within the browser’s visible viewport. Usually, this exception occurs when mouse movement actions are involved, such as ‘dragAndDrop‘ or ‘moveToElement‘. The element on which some user action is intended is not within the browser’s visible viewport; hence, some scrolling must be done to access the element.

Reasons for MoveTargetOutOfBoundsException

Below are the common reasons for MoveTargetOutOfBoundsException in Selenium:

Element is out of the browser’s viewport

The web element to be accessed in the Selenium script is present on the screen; however, it is currently out of the visible area of the browser window. The element could be located too far to the right/left or down on the screen and not visible. This requires scrolling through the browser window to bring the element within the browser’s viewport.

Example:
WebDriver driver = new ChromeDriver();
driver.get("http://example.com");

WebElement element = driver.findElement(By.id("offscreenElement")); // Element is off-screen
new Actions(driver).moveToElement(element).click().build().perform(); // Throws MoveTargetOutOfBoundsException

Element co-ordinates are not accessible

Consider you are using moveByOffset to provide the element coordinates to move the mouse pointer. In this case, if the element is outside the viewport, MoveTargetOutOfBoundsException is thrown.

Example:
WebDriver driver = new ChromeDriver();
driver.get("http://example.com");
new Actions(driver).moveByOffset(2000,2000).click().build().perform(); // Throws MoveTargetOutOfBoundsException

Dynamic content

In dynamic webpages, the content is dynamic; hence, the position of the web elements is also dynamic and changes. Now, the exception is thrown if the element moves after the dynamic location is calculated, and then the script tries to access the element based on the old calculated location.

Example:
WebDriver driver = new ChromeDriver();
driver.get("http://example.com/dynamic-content-page");
WebElement element = driver.findElement(By.id("dynamicElement"));

// Assuming the element moves due to dynamic content loading
new Actions(driver).moveToElement(element).click().build().perform(); // Might throw MoveTargetOutOfBoundsException

Fixed (sticky) UI elements

Some elements are fixed on the UI, such as header/footers. They are called fixed or sticky elements. These elements can interfere with the coordinate calculation of the other UI elements, leading to MoveTargetOutOfBoundsException.

Example:
WebDriver driver = new ChromeDriver();
driver.get("http://example.com/page-with-fixed-header");
WebElement elementBelowHeader = driver.findElement(By.id("elementBelowFixedHeader"));

// The fixed header might interfere with the actual location of this element
new Actions(driver).moveToElement(elementBelowHeader).click().build().perform();
// Might throw MoveTargetOutOfBoundsException

iFrame context switch

If the Selenium script tries to interact with an element within an iFrame and the context is not switched. Then, in this case, an exception will occur.

Example:
WebDriver driver = new ChromeDriver();
driver.get("http://example.com/page-with-iframe");

// Directly trying to interact with an element inside an iframe
WebElement elementInIframe = driver.findElement(By.id("elementInIframe")); // Throws NoSuchElementException or MoveTargetOutOfBoundsException
// The correct approach is to first switch to the iframe

Resolution for MoveTargetOutOfBoundsException

Below are a few ways to resolve the MoveTargetOutOfBoundsException in Selenium:

Scroll to the element

The simplest way to handle MoveTargetOutOfBoundsException is to scroll to the element and bring it within the browser’s visible viewport.

Example:
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

WebDriver driver = new ChromeDriver();
WebElement element = driver.findElement(By.id("element_id"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Actions actions = new Actions(driver);
actions.moveToElement(element).perform();

Adjust browser window size

Another option is to adjust the browser window size to make sure that the browser window is big enough to include the UI element within the browser’s viewport.

Example:
driver.manage().window().setSize(new Dimension(1024, 768));

Use JavaScript direct interactions

If you can still not interact normally with the element, then as a workaround, use JavaScript to interact directly with the element.

Example:
import org.openqa.selenium.JavascriptExecutor;

WebElement element = driver.findElement(By.id("element_id"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

Correctly handle dynamic content

To handle the dynamic content and the dynamic locations of the elements, use explicit waits to ensure that the page has finished loading and that dynamic elements have stabilized.

Example:
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("element_id")));

Switch to correct iFrame

If the element is inside an iframe, switch the WebDriver context to that iframe before performing any action on the web element.

Example:
driver.switchTo().frame("iframeNameOrID");

WebElement element = driver.findElement(By.id("element_id_inside_iframe"));

// Interact with the element here
driver.switchTo().defaultContent(); // Switch back to the main document

How testRigor is an Intelligent Alternative?

Selenium has enjoyed being the most popular test automation tool for years. Being open-source and a vast community has contributed to the success. Here is a Selenium with Java cheat sheet for you to refer to.

However, with faster delivery cycles in Agile environments, Selenium is becoming an overhead with enormous exceptions and maintenance issues. Here are the 11 reasons why not to use Selenium anymore.

AI-powered, codeless, intelligent test automation tools such as testRigor are game changers in the test automation paradigm. Anyone in your team can write the test cases in plain English without any pre-requisite programming knowledge using NLP and generative AI capabilities of testRigor.

You need not deliberately put explicit waits in your test case to wait for the elements to load entirely before any interaction. That is the power of AI; testrigor manages all these waits automatically for you. Specify the UI elements as you see them on the screen and not through tedious XPath/ CSS identifiers.

For example, if you need to click the ‘Add to Cart’ button on the screen. Use the UI text you see on the button and write the test in plain English.
click on "Add to cart"
Sometimes, there will be multiple elements with the same title on the page, for example, multiple “Add to cart” buttons. You can reference them via a number like this:
click on the 3rd "Add to cart"
Remember how there might be multiple focus areas on the page? You can easily combine indexes with relative locations:
click on the second "Add to Cart" within the context of "Section 1"

Quickly scroll through the page as testRigor supports scroll in all four directions: up, down, left, and right.

You can also scroll partially (¼, ½, 1) for better accuracy and speed of execution:
scroll up by ¼ of the screen
You can scroll directly to a specific part of the page:
scroll up until page contains “Place order”
Or start scrolling from a specific part of the page:
scroll up on “Contact Us”

Conclusion

Selenium exceptions are annoying and time-consuming. There is no second thought to this. Engineers do not want to waste time resolving these issues using workarounds and get stuck in laborious test script maintenance.

When there are better alternatives that use AI to resolve these issues for you, why not use them? Try testRigor to see how liberating it is when a tool singlehandedly performs all your web, mobile, desktop, and API testing in plain English. Bypass all these Selenium exceptions and errors and only focus on building robust tests, achieving excellent test coverage and scalability.

You're 15 Minutes Away From Automated Test Maintenance and Fewer Bugs in Production
Simply fill out your information and create your first test suite in seconds, with AI to help you do it easily and quickly.
Achieve More Than 90% Test Automation
Step by Step Walkthroughs and Help
14 Day Free Trial, Cancel Anytime
“We spent so much time on maintenance when using Selenium, and we spend nearly zero time with maintenance using testRigor.”
Keith Powe VP Of Engineering - IDT
Related Articles

SAP Concur Testing

Concur Technologies, now SAP Concur, is an American software-as-a-service (SaaS) company providing travel and expense management ...

Authorize.net Testing

You need a payment gateway in several scenarios, especially when you are running a business that requires secure, efficient, and ...

KYC Portal CLM Testing

KYC Portal CLM is an award-winning Client Lifecycle Management (CLM) platform designed to streamline and automate the Know Your ...
On our website, we utilize cookies to ensure that your browsing experience is tailored to your preferences and needs. By clicking "Accept," you agree to the use of all cookies. Learn more.
Cookie settings
Privacy Overview
This site utilizes cookies to enhance your browsing experience. Among these, essential cookies are stored on your browser as they are necessary for ...
Read more
Strictly Necessary CookiesAlways Enabled
Essential cookies are crucial for the proper functioning and security of the website.
Non-NecessaryEnabled
Cookies that are not essential for the website's functionality but are employed to gather additional data. You can choose to opt out by using this toggle switch. These cookies gather data for analytics and performance tracking purposes.