如何下载无头Chrome(硒)中的文件?

时间:2019-04-26 14:08:07

标签: python selenium google-chrome-headless

我只想从带有硒的网站上下载文件。 我的代码可以在Chrome上很好地工作,但不适用于仅用于下载部分的无头chrome。 我刚刚找到有关该问题的主题: Chrome headless file download with Selenium in Python 因此,我在代码中添加了3行,但仍然无法正常运行。 这是我的无头代码 (通过屏幕可以确保代码在下载部分之前可以正常工作。):


    ### headless part
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-gpu')
    driver = webdriver.Chrome(options=options)

    # part add from the other topic
    driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
    params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': 'path_for_download'}}
    driver.execute("send_command", params)
    # end part add

    driver.implicitly_wait(20)
    ### end headless


    driver.get("thewebsite")
    button = driver.find_element_by_id('menutop')
    items = button.find_elements_by_tag_name("li")
    ActionChains(driver).move_to_element(items[0]).perform()
    ActionChains(driver).click().perform()
    button = driver.find_element_by_id('coremenu2')
    ActionChains(driver).move_to_element(button).perform()
    ActionChains(driver).click().perform()
    driver.find_element_by_xpath("//input[@value='excel']").click()
    driver.find_element_by_xpath("//input[@value='Generar']").click()
    time.sleep(5)
    driver.save_screenshot("screenexcel.png")

因此,如果您有解决方案,或者可以向我解释我在上面发布的另一个主题以及为什么它不起作用,那么解决方案将对我有帮助

0 个答案:

没有答案
相关问题