Search code examples
seleniumwebdriver

When performing mouse action for second time, I get MoveTargetOutOfBounds Exeption


Im using ActionChains from selenium webdriver to click on particular spot on the canvas. First time it works, but second, it throw an exception:

actions = ActionChains(driver)
actions.move_by_offset(650,500).click().perform()
time.sleep(2)
actions.move_by_offset(700,500).click().perform()

even if I change the values in second move_by_offset to (0,0) it gives an error:

Traceback (most recent call last):
File "/home/*****1.py", line 55, in <module>
actions.move_by_offset(700,500).click().perform()
File "/home/kuba/*****/action_chains.py", line 80, in perform
self.w3c_actions.perform()
File "/home/kuba/******/common/actions/action_builder.py", line 76, in perform
self.driver.execute(Command.W3C_ACTIONS, enc)
File "/home/*****python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/*******/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds
(Session info: chrome=84.0.4147.89)

PLEASE NOTE: the place I'm clicking on is a canvas - I can't even get a menu on it when rightmouse button is pressed. Does the issue have something to do that this canvas gets focus from webdriver?


Solution

  • There are 2 issues here: you need to reser actions with

    actions.reset_actions()
    

    the mouse moves from the last point, so giving values of (700,500) moved it outside of the screen = exception Here is the working code:

    actions = ActionChains(driver)
    actions.move_by_offset(650,500).click().perform()
    actions.reset_actions()
    actions.move_by_offset(80,0).click().perform()