1

I'm using Python 3.9 with the latest Selenium and have this code, which runs fine on my Mac, Chrome driver 101 headless instance of my script ...

  element = self.driver.find_element(By.CSS_SELECTOR, "body")
  actions = ActionChains(self.driver)
  actions.move_to_element_with_offset(element, 0, 0).perform()

However, when I run this same code on my CentOS 7 instance, with chromedriver 99 (latest available), I get this error

>       raise exception_class(message, screen, stacktrace)
E       selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds
E         (Session info: headless chrome=99.0.4844.84)

/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py:242: MoveTargetOutOfBoundsException

Any thoughts on what this means or what additional configurations I may need to make on my CentOS 7 setup? Happy to rewrite the code as long as it runs on both environments.

Dave
  • 15,639
  • 133
  • 442
  • 830

2 Answers2

0

From Selenium documentation for MoveTargetOutOfBoundsException:

Indicates that the target provided to the actions move() method is invalid - outside of the size of the window.

From Actions class documentation:

moveByOffset​(int xOffset, int yOffset)
Moves the mouse from its current position (or 0,0) by the given offset.

This issue looks odd for me as we are talking about the body element, and somehow it is not in the visible part of your screeen, you can try:

  1. To scroll to your element - with JavaScript or Selenium

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

  1. To wait until the element is loaded
  2. Different offset - positive/negative.
  3. Get the scrollheight/width of the window and the X and Y coordinates of the body - this can help you debug the issue.

document.body.scrollHeight

window.innerHeight

  1. You can try to first click or hover the element.

References:
How to get the browser viewport dimensions?
https://www.lambdatest.com/automation-testing-advisor/selenium/classes/org.openqa.selenium.interactions.MoveTargetOutOfBoundsException
Selenium - MoveTargetOutOfBoundsException with Firefox https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebElement.html#getLocation()

K. B.
  • 3,342
  • 3
  • 19
  • 32
  • Do you know of an equivalent statement "((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);" that would work for Python? I'm having trobule translating your answer into something I can put in my Python script. – Dave May 26 '22 at 23:52
  • driver.execute_script("arguments[0].scrollIntoView();", element) https://stackoverflow.com/questions/41744368/scrolling-to-element-using-webdriver – K. B. May 27 '22 at 06:06
0

move_to_element_with_offset method is used to move the mouse by an offset of the specified element. Offsets are relative to the top-left corner of the element. In general, when we move any element manually at that time also, we need to click on the element then move it. Hope this will help, if not please let me know.

  element = self.driver.find_element(By.CSS_SELECTOR, "body")
  actions = ActionChains(self.driver)
  actions.move_to_element_with_offset(element, 0, 0).click().perform()
AmitKS
  • 132
  • 4
  • Gave this a whirl, but still getting the error, "selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds" – Dave May 26 '22 at 23:49