Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium - MoveTargetOutOfBoundsException with Firefox

Tags:

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)  
like image 922
Matheus Avatar asked Jun 27 '17 09:06

Matheus


People also ask

Can I use Selenium with Firefox?

Selenium IDE by SeleniumIt is implemented as a Firefox extension, and allows you to record, edit, and debug tests.

How do I open Firefox browser with GeckoDriver in Selenium?

Step 1: Navigate to the official Selenium website. Under third-party drivers, one will find all the drivers. Just click on the Mozilla GeckoDriver documentation as shown below. Now, it will navigate to the GeckoDriver downloads link, where one can download the suitable driver based on the OS as it is platform agnostic.


2 Answers

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() 
like image 175
Cynic Avatar answered Oct 11 '22 03:10

Cynic


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.

like image 35
undetected Selenium Avatar answered Oct 11 '22 01:10

undetected Selenium