Selenium - MoveTargetOutOfBoundsException with Firefox
Asked Answered
B

9

33

I'v got problem with function move_to_element on Firefox Webdriver (Chrome, IE works well)

driver = webdriver.Firefox()
driver.get("https://stackoverflow.com")
time.sleep(5)
source_element = driver.find_element_by_xpath('//*[@id="footer"]/div/ul/li[1]/a')
ActionChains(driver).move_to_element(source_element).perform()

I am working with these versions: geckodriver - 0.17.0 // Firefox - 54.0 // selenium - 3.4.3

After running this script, on output shows:

selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (134.96666717529297, 8682.183013916016) is out of bounds of viewport width (1268) and height (854) 
Bullpup answered 27/6, 2017 at 9:42 Comment(0)
F
11

This error...

selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (134.96666717529297, 8682.183013916016) is out of bounds of Viewport width (1268) and height (854)

...implies that the element you are looking for is not within the Viewport. We need to scroll down to bring the element within the Viewport. Here is the working code:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
caps = DesiredCapabilities().FIREFOX
caps["marionette"] = True
driver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("https://stackoverflow.com")
last_height = driver.execute_script("return document.body.scrollHeight")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
source_element = driver.find_element_by_xpath('//*[@id="footer"]/div/ul/li[1]/a')
ActionChains(driver).move_to_element(source_element).perform()

Let me know if this Answers your Question.

Fiche answered 27/6, 2017 at 10:32 Comment(2)
what is last_height for ?Gregoriagregorian
Adding a simple element.click() before the actions, worked for me :)Marital
F
37

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()
Fradin answered 27/8, 2018 at 18:40 Comment(11)
Oh man, they might finally fix this: w3.org/2018/10/26-webdriver-minutes.html#resolution01Fradin
I tried using your function, but it isn't scrolling at all. I then get the expected MoveTargetOutOfBoundsException when I try to access the element. Do I need to change anything for Python 3?Whitmer
You need to pass in the element and driver but it should just work. What browser are you using? Also try doing something like import time and time.sleep(3) after you call it so you can see it scrollFradin
I'm using Firefox. I'll try the sleeps. EDIT: Sleeps didn't help. I'm passing it the driver and element, and I know the function is running, it just doesn't scroll at all. I'm in a unique position where only part of the frame is scrollable, so I'm clicking in that area before trying this function. I guess that part may be failing.Whitmer
I ended up using <driver.execute_script("arguments[0].scrollIntoView();", element)> and it actually worked fine. Not sure why that worked but your function didn't.Whitmer
Yeah, the exception is happening when you click. So if you don't use the function to scroll to it before you click you would get the exception. The function works fine in Firefox. Asked about browser as I haven't tested it IE but it's basic javascript so I would expect it should work.Fradin
I thought your scroll_shim function was supposed to the scrolling?Whitmer
It does but you have to scroll before you click. So it wasn't working because you were hitting the error before you were calling the function. I am using this with Firefox and it works.Fradin
I had a time.sleep between scroll_shim and the click call. It's probably just related to the weirdness of the HTML I was working with, so I wouldn't worry about it.Whitmer
@Fradin I see that w3 has updated some documents on this here: drafts.csswg.org/cssom-view/#dom-element-scrollintoview Do you know if there is any implementation of this yet? I've got a bunch of code for testing in Chrome that I was trying to use for FireFox, and I don't want to rewrite the existing code if I can help it.Scram
Eliezer Miron in a previous comment mentioned how you could use driver.execute_script("arguments[0].scrollIntoView(); instead. But as far as them fixing it in selenium, I doubt anything would happen before Selenium 4, even with that, it doesn't sound like Firefox would make move_to_element consistent. We'd probably just have to add something like scroll_to_element to all our action chains to support all browsers. So the fix would likely be similar to creating your own scroll shim and pasting it in before your clicks.Fradin
F
11

This error...

selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (134.96666717529297, 8682.183013916016) is out of bounds of Viewport width (1268) and height (854)

...implies that the element you are looking for is not within the Viewport. We need to scroll down to bring the element within the Viewport. Here is the working code:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
caps = DesiredCapabilities().FIREFOX
caps["marionette"] = True
driver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("https://stackoverflow.com")
last_height = driver.execute_script("return document.body.scrollHeight")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
source_element = driver.find_element_by_xpath('//*[@id="footer"]/div/ul/li[1]/a')
ActionChains(driver).move_to_element(source_element).perform()

Let me know if this Answers your Question.

Fiche answered 27/6, 2017 at 10:32 Comment(2)
what is last_height for ?Gregoriagregorian
Adding a simple element.click() before the actions, worked for me :)Marital
S
6

You can try below while automating the script in Firefox when it usually throws MoveTargetOutOfBoundsException error:

One can transform/Zoom-in or out by

driver.execute_script("document.body.style.transform='scale(0.9)';")

Sometimes if you are running automation script in Jenkins(CI tools), you might also face the issue from above transform code where content of browser is scaled out not the actual browser, in those condition you can try out to resize the browser window:

driver.set_window_size(x, y)

or

driver.set_window_size(2000, 694)
Springhead answered 5/8, 2019 at 8:24 Comment(1)
1) Transforming scale to zoom-out did not work for me as the zooming-out does not automatically shift the small content into view. 2) Window resizing did not work for me either as the window size seems to be limited by my laptop screen size and thus the resized window just automatically get resized to my laptop's screen size.Tramway
U
1

time.sleep() command can be used sometimes when an elements is taking time to switch.Scrolling always doesn't help you.

Underbodice answered 12/6, 2020 at 6:45 Comment(0)
T
1

I am using this to avoid getting MoveTargetOutOfBoundsException with running Firefox. (in C#):

public void ScrollToElement(IWebElement elementLocator)
{

    try
    {
        action.MoveToElement(elementLocator).Perform();
    }

    catch(MoveTargetOutOfBoundsException e)
    {
        Console.WriteLine(e);
        js.ExecuteScript("arguments[0].scrollIntoView(true);", elementLocator);
    }

}

In Python it would probably be like this:

def scroll_to_element(driver, element_locator):
    actions = ActionChains(driver)
    try:
        actions.move_to_element(element_locator).perform()
    except MoveTargetOutOfBoundsException as e:
        print(e)
        driver.execute_script("arguments[0].scrollIntoView(true);", element_locator)
Thicken answered 6/7, 2022 at 22:6 Comment(0)
E
1

Note that if you're encountering this problem while using move_to_element to trigger an event like a mouseover or mouseenter, you can skip using move_to_element entirely.

You can instead use execute_script to call the event directly:

driver = webdriver.Firefox()
driver.get("https://stackoverflow.com")
source_element = driver.find_element_by_xpath('//*[@id="footer"]/div/ul/li[1]/a')
driver.execute_script("arguments[0].dispatchEvent(new Event('mouseover'));", source_element)

It works by using javascript to dispatch the event, and it will work even if the element isn't in the viewport.
See also the dispatchEvent docs for more detail.

Eczema answered 15/8, 2022 at 21:51 Comment(0)
G
0

This is not a generic solution, but might help ring a bell. Thanks to the inputs in this thread, I could resolve the issue by tweaking the target page.

The exception, in my case, occurred :

  • while checking for existence of an element that happened to be at the top of the page,
  • while the page, when rendered on the browser, was getting auto-scrolled to the bottom due to "autofocus" that was set on a button element at the bottom of the page.

Because the test script was against my own development, I had the flexibility to review the need for the "autofocus" and avoid the auto-scroll to the bottom of the page when the page got rendered.

Alternatively, checking for existence of a different element, that gets rendered within the viewport, if feasible, could also resolve the issue.

Grissel answered 23/7, 2020 at 11:24 Comment(0)
A
0

You can also use the code below. I used it, the program works fine

lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
match=False
while(match==False):
    lastCount = lenOfPage
    time.sleep(3)
    lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
    if lastCount == lenOfPage:
        match=True
time.sleep(5)

Then later

actions = ActionChains(driver)
actions.move_to_element(source_element)
actions.click()
actions.perform()

Besides you can add your code:

from selenium.common.exceptions import StaleElementReferenceException, MoveTargetOutOfBoundsException

and use try except script.

For example:

try:
    'Main Code'
            
except (StaleElementReferenceException, MoveTargetOutOfBoundsException):
    pass
Alsace answered 22/7, 2022 at 13:41 Comment(0)
Y
-3

I was searching for the solution for last 2 hours and nothing was working. Then I just gave a sleep time time.sleep(5) after the window opened on which action is to be performed and it started working.

Youthen answered 5/4, 2020 at 12:37 Comment(2)
That's just bad solution.Wolverhampton
Should be wrapped with WebDriverWait.Shoebill

© 2022 - 2024 — McMap. All rights reserved.