我在 Firefox* Webdriver 上的move_to_element 函数遇到问题(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)
这个错误…
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (134.96666717529297, 8682.183013916016) is out of bounds of Viewport width (1268) and height (854)
…表示您要查找的元素不在Viewport中。我们需要向下滚动以将元素带入视口中。这是工作代码:
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()
让我知道这是否回答了您的问题。