【问题标题】:Python/Selenium - Can not download file in headless modePython/Selenium - 无法以无头模式下载文件
【发布时间】:2021-04-29 11:14:31
【问题描述】:

我在下载此文件时遇到问题:

https://www.vidahomeloans.co.uk/intermediaries/documents

  • 住宅
  • 产品指南和价格表
  • 住宅价格表 -> 然后按旁边的下载

如果我在不使用无头模式的情况下下载,那么一切正常,但在无头模式下,我会收到 example.pdf 文件

任何想法为什么会发生?

这是我目前用来下载文件的(按下实际按钮,使过程通用)

options.add_experimental_option('prefs',{
    'credentials_enable_service': False,
    'plugins':{
        'always_open_pdf_externally': True
    },
    'profile': {
        'password_manager_enabled': False,
    },
    'download': {
        'prompt_for_download': False,
        'directory_upgrade': True,
        'default_directory': ''
    }
})

【问题讨论】:

    标签: python selenium headless


    【解决方案1】:

    您可以使用urllib library 下载该 pdf 文件

    import urllib.request
    pdf_url = "https://www.vidahomeloans.co.uk/docs/default-source/product-guide-rate-sheets/residential-rate-sheet.pdf?sfvrsn=27d0a846_16"
    
    
    def download_file(download_url, filename):
        response = urllib.request.urlopen(download_url)
        file = open(filename + ".pdf", 'wb')
        file.write(response.read())
        file.close()
    
    
    download_file(pdf_url, "Output")
    
    

    【讨论】:

    • 我已经更新了我的问题,关于下载文件,我正在按下按钮
    猜你喜欢
    • 2022-09-23
    • 2018-06-11
    • 2023-01-21
    • 2023-01-17
    • 2020-05-29
    • 2016-07-29
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    相关资源
    最近更新 更多