Selenium – MoveTargetOutOfBoundsException with Firefox

I think the correct answer here got lucky that the element they were looking for happened to be at the bottom of the page and didn’t really explain why this occurs in Firefox commonly.

Browsers other than Firefox treat Webdrivers move_to_element action as scroll to part of page with element then hover over it. Firefox seems to have taken a hardline stance that move_to_element is just hover and are waiting for a scroll action to fix this.

For now you have to workaround this bug using javascript as mentioned in previous answer, but I suggest using something like this instead of arbitrarily (well I guess the example was a footer) scrolling to bottom of page and hoping object is still in view.

    def scroll_shim(passed_in_driver, object):
        x = object.location['x']
        y = object.location['y']
        scroll_by_coord = 'window.scrollTo(%s,%s);' % (
            x,
            y
        )
        scroll_nav_out_of_way = 'window.scrollBy(0, -120);'
        passed_in_driver.execute_script(scroll_by_coord)
        passed_in_driver.execute_script(scroll_nav_out_of_way)

Then later

source_element = driver.find_element_by_xpath('//*[@id="footer"]/div/ul/li[1]/a')
if 'firefox' in driver.capabilities['browserName']:
    scroll_shim(driver, source_element)
# scroll_shim is just scrolling it into view, you still need to hover over it to click using an action chain.
actions = ActionChains(driver)
actions.move_to_element(source_element)
actions.click()
actions.perform()

Leave a Comment