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.
2
Answers
From Selenium documentation for MoveTargetOutOfBoundsException:
From Actions class documentation:
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:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
document.body.scrollHeight
window.innerHeight
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()
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.