简体   繁体   English

Python Selenium Headless下载

[英]Python Selenium Headless download

I'm trying to download a file with selenium. 我正在尝试使用硒下载文件。 I've searched everything. 我已经搜索了所有内容。

At How to control the download of files with Selenium Python bindings in Chrome some people told that it worked. Chrome中如何使用Selenium Python绑定控制文件下载时,有人告诉它可以使用。 But it didn't worked for me! 但这对我没有用! Maybe I miss something? 也许我想念什么? The only things differentlly is that my page autostarted download the csv file. 唯一不同的是,我的页面自动启动下载了csv文件。

After studying the chrome codes I added: 研究了Chrome代码后,我添加了:

        "safebrowsing_for_trusted_sources_enabled": False

But still id didn't worked. 但仍然无法正常工作。

options = Options()
options.add_argument("--disable-notifications")
options.add_argument('--no-sandbox')
options.add_experimental_option("prefs", {
    "download.default_directory": "C:\\Users\\claudiu.ivanescu\\Downloads",
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing_for_trusted_sources_enabled": False
})
options.add_argument('--disable-gpu')
options.add_argument('--disable-software-rasterizer')
options.add_argument('--headless')

Thank for support 感谢支持

If anybody interested, after 2 days of search :). 如果有人感兴趣,请在搜索2天后:)。 I manage to make it works! 我设法使其有效!

I found the answer the bug tracking in this comment: https://bugs.chromium.org/p/chromium/issues/detail?id=696481#c86 我在此评论中找到了错误跟踪的答案: https : //bugs.chromium.org/p/chromium/issues/detail?id=696481#c86

The code I used is: 我使用的代码是:

def enable_download_headless(browser,download_dir):
    browser.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
    params = {'cmd':'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
    browser.execute("send_command", params)

if __name__ == '__main__':
    options = Options()
    options.add_argument("--disable-notifications")
    options.add_argument('--no-sandbox')
    options.add_argument('--verbose')
    options.add_experimental_option("prefs", {
        "download.default_directory": "C:\\tmp",
        "download.prompt_for_download": False,
        "download.directory_upgrade": True,
        "safebrowsing_for_trusted_sources_enabled": False,
        "safebrowsing.enabled": False
    })
    options.add_argument('--disable-gpu')
    options.add_argument('--disable-software-rasterizer')
    options.add_argument('--headless')
    driver_path = "C:\\Users\\tmp\\chromedriver.exe"
    driver = webdriver.Chrome(driver_path, chrome_options=options)
    enable_download_headless(driver, "C:/tmp")
    driver.get(url)

Maybe will be some use to others in the future... Probably is a lot of useless things inside, but didn't have time yet to change :). 也许将来会对其他人有所用...里面可能有很多没用的东西,但是还没有时间来改变:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM