首页 > 解决方案 > 具有无头模式的docker容器中的python selenium + geckodriver无法滚动页面

问题描述

我正在尝试在 docker 容器 ubuntu 18.04 中以无头模式使用 geckodriver 运行 selenium。这是我的代码:

    while True:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        time.sleep(2)
        newHeight = driver.execute_script("return document.body.scrollHeight")
        print('scrolling..')
        if newHeight == lastHeight:
            print(f'scrolling done..')
            list_of_images = driver.find_elements_by_css_selector('._2eea a')
            print(f'collecting: {fp_url}')
            images = []
            for image in list_of_images:
                url = image.get_attribute('href')
                if 'type=3' in str(url):
                    print(f'append: {url}')
                    images.append(url)
                    # clear_memory()
            print(f'total: {len(images)} memes')
            count = 1
        else:
            lastHeight = newHeight

当我在本地计算机上尝试时,我没有收到任何错误,但是当我在 docker 容器中尝试时,页面似乎不会滚动。这是我的驱动程序设置:

    options = webdriver.FirefoxOptions()
    options.add_argument('--hide-scrollbars')
    options.add_argument('--disable-gpu')
    options.add_argument('-headless')
    driver = webdriver.Firefox(firefox_options=options, executable_path=os.path.join(os.getcwd(), "geckodriver"))

标签: pythonseleniumdockerfirefoxgeckodriver

解决方案


假设您的 chrome 驱动程序完全是最新的,但值得检查

https://sites.google.com/a/chromium.org/chromedriver/downloads

也许尝试几种不同的滚动方式可以产生更好的结果:)

使用动作链 - https://selenium-python.readthedocs.io/api.html

from selenium.webdriver.common.action_chains import ActionChains as AC

ele = driver.find_element_by_id("myID")

actions = AC(driver)
actions.move_to_element(ele).perform()

将元素 id 作为参数传递给 scrollintoview()

driver.execute_script("arguments[0].scrollIntoView();", ele)

推荐阅读