Selenium - Firefox 的 MoveTargetOutOfBoundsException [英] Selenium - MoveTargetOutOfBoundsException with Firefox

查看:19
本文介绍了Selenium - Firefox 的 MoveTargetOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Firefox Webdriver(Chrome、IE 运行良好)上的 move_to_element 功能出现问题

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()

我正在使用这些版本:geckodriver - 0.17.0//Firefox - 54.0//selenium - 3.4.3

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) 

推荐答案

这个错误...

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.

这篇关于Selenium - Firefox 的 MoveTargetOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆