我在 Firefox 上运行 move_to_element 问题Webdriver(Chrome,IE运行良好)
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
运行此脚本后,输出显示:
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (134.96666717529297, 8682.183013916016) is out of bounds of viewport width (1268) and height (854)
3 回答
我认为这里的正确答案很幸运,他们正在寻找的元素恰好位于页面的底部,并没有真正解释为什么这种情况常见于Firefox .
除Firefox之外的浏览器将Webdrivers
move_to_element
action视为滚动到带有元素的页面的一部分,然后将鼠标悬停在它上面 . Firefox似乎采取了hardline stance,即move_to_element只是悬停,正在等待scroll动作来解决这个问题 .现在你必须使用前面的答案中提到的javascript来解决这个bug,但我建议使用类似这样的东西而不是任意(我猜这个例子是一个页脚)滚动到页面底部并希望对象仍在视图中 .
然后呢
尝试将窗口大小设置为驱动程序BEFORE driver.get()
以下是您的问题的答案:
您看到的错误将其全部显示为
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (134.96666717529297, 8682.183013916016) is out of bounds of Viewport width (1268) and height (854)
. 您要查找的元素不在Viewport内 . 我们需要向下滚动以将元素带入Viewport . 这是工作代码:如果这回答你的问题,请告诉我 .