【问题标题】:Python Selenium Headless downloadPython Selenium Headless 下载
【发布时间】:2018-10-16 07:30:23
【问题描述】:

我正在尝试使用 selenium 下载文件。我已经搜索了所有内容。

How to control the download of files with Selenium Python bindings in Chrome 有人告诉它有效。 但这对我不起作用!也许我错过了什么?唯一不同的是我的页面自动开始下载 csv 文件。

研究完我添加的chrome代码:

        "safebrowsing_for_trusted_sources_enabled": False

但 id 仍然没有工作。

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')

感谢支持

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    如果有人感兴趣,经过 2 天的搜索 :)。我设法让它工作!

    我在这条评论中找到了错误跟踪的答案:https://bugs.chromium.org/p/chromium/issues/detail?id=696481#c86

    我使用的代码是:

    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)
    

    也许将来对其他人有用... 大概是里面很多没用的东西,还没来得及改:)。

    【讨论】:

    • 嘿,我尝试运行你的代码,它没有产生任何错误但也没有下载文件。在调用enable_download_headless(driver, ".") 之后,我调用了btn_dl.click(),但它什么也没做。
    • Selenium 这是一件困难的事情。 2年前的chromedriver对我有用。您必须检查版本之间的兼容性。
    • Voodoo 魔法,不过是一种有效的 voodoo 魔法。
    • 尝试了很多这些参数的配置,直到我找到你的帖子才能让下载工作。谢谢!这可能是我需要的:safebrowsing_for_trusted_sources_enabled": False
    • Hai @Claudiu - 经过长时间的搜索,这对我有用并节省了我的时间。
    猜你喜欢
    • 2020-04-08
    • 2017-12-26
    • 1970-01-01
    • 2018-01-19
    • 2023-01-14
    • 1970-01-01
    • 2019-05-25
    • 2019-01-25
    • 2021-01-28
    相关资源
    最近更新 更多