首页 文章

Selenium - 使用Firefox的MoveTargetOutOfBoundsException

提问于
浏览
4

我在 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 回答

  • 3

    我认为这里的正确答案很幸运,他们正在寻找的元素恰好位于页面的底部,并没有真正解释为什么这种情况常见于Firefox .

    除Firefox之外的浏览器将Webdrivers move_to_element action视为滚动到带有元素的页面的一部分,然后将鼠标悬停在它上面 . Firefox似乎采取了hardline stance,即move_to_element只是悬停,正在等待scroll动作来解决这个问题 .

    现在你必须使用前面的答案中提到的javascript来解决这个bug,但我建议使用类似这样的东西而不是任意(我猜这个例子是一个页脚)滚动到页面底部并希望对象仍在视图中 .

    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)
    

    然后呢

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

    尝试将窗口大小设置为驱动程序BEFORE driver.get()

    driver.set_window_size(1024, 768) #(1920, 1080) #(4096, 3112)
    
  • 2

    以下是您的问题的答案:

    您看到的错误将其全部显示为 selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (134.96666717529297, 8682.183013916016) is out of bounds of Viewport width (1268) and height (854) . 您要查找的元素不在Viewport内 . 我们需要向下滚动以将元素带入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()
    

    如果这回答你的问题,请告诉我 .

相关问题