【问题标题】:python selenium + geckodriver in docker container with headless mode cannot scroll page具有无头模式的docker容器中的python selenium + geckodriver无法滚动页面
【发布时间】:2019-03-24 10:33:30
【问题描述】:

我正在尝试在 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"))

【问题讨论】:

    标签: python selenium docker firefox geckodriver


    【解决方案1】:

    假设您的 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)
    

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 2019-05-13
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多