2

Using the following code:

elem = driver.find_element_by_xpath('/html/body/canvas')
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element_with_offset(elem, 185, -35).click().perform()

I am unable to navagate to the desired section of a canvas element and receive this error:

MoveTargetOutOfBoundsException: move target out of bounds
(Session info: chrome=85.0.4183.102)

My move target is most definitely within the viewport, no scrolling is necessary to make it clickable. I am using chromedriver and am using the top left corner of the canvas as the start point for my pixel coordinates for move_to_element_with_offset(). Any ideas to fix this? I'm interested in any solutions to click a specified point on a canvas in python, doesn't need to use this same method.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

move_to_element_with_offset()

move_to_element_with_offset() moves the mouse by an offset of the specified element where offsets are relative to the top-left corner of the element.

So from the top-left corner of the <canvas> element if you intent to move by offset (185, -35) where:

  • 185 units to the right
  • 35 units upwards

will be always result into MoveTargetOutOfBoundsException.


Solution

Instead of a negative offset to move upwards you can use a possitive offset to move downwards as follows:

actions.move_to_element_with_offset(elem, 35, 35).click().perform()

References

You can find a relevant detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352