Selenium Webdriver: How to Download a PDF File with Python?

前端 未结 3 1967
离开以前
离开以前 2020-12-03 19:17

I am using selenium webdriver to automate downloading several PDF files. I get the PDF preview window (see below), and now I would like to download the file. How can I accom

3条回答
  •  被撕碎了的回忆
    2020-12-03 19:54

    You can download the pdf (Embeded pdf & Normal pdf) from web using selenium.

    from selenium import webdriver
    
    download_dir = "C:\\Users\\omprakashpk\\Documents" # for linux/*nix, download_dir="/usr/Public"
    options = webdriver.ChromeOptions()
    
    profile = {"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}], # Disable Chrome's PDF Viewer
                   "download.default_directory": download_dir , "download.extensions_to_open": "applications/pdf"}
    options.add_experimental_option("prefs", profile)
    driver = webdriver.Chrome('C:\\chromedriver\\chromedriver_2_32.exe', chrome_options=options)  # Optional argument, if not specified will search path.
    
    driver.get(`pdf_url`)
    

    It will download and save the pdf in directory specified. Change the download_dir location and chrome driver location as per your convenience.

    You can download chrome driver from here.

    Hope it helps!

提交回复
热议问题