Search code examples
python-3.xseleniumselenium-chromedrivermacos-big-surselenium-webdriver-python

With Selenium on CentOS 7, getting a MoveTargetOutOfBoundsException on my CentOS 7, which doesn't occur when the script is run on Mac OS Big Sur


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.


Solution

  • 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()