pythongoogle-chromeselenium-webdriverpdf

Downloading PDF using Selenium and Python in Chrome: Disabling PDF Viewer


What I see currentlyI am trying to download a pdf after clicking on the button to do so but by default Chrome will use PDF Viewer. I have implemented the following code, which seems to stop the viewer from showing the PDF itself, but the frame of the viewer remains and a button exists in order to download. I am stuck because this button element does not show in the page source for Selenium to read. So I either need a solution to read this additional element or implement a better solution to disabling the PDF viewer completely.

options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
"download.default_directory": "file_path", #Change default directory for downloads
"download.prompt_for_download": False, 
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True #It will not show PDF directly in chrome 
})
wd = webdriver.Chrome(options=options)

enter image description here

I have tried this code. I need suggestions to fix. thank you


Solution

  • You can try:

    1. Set the option plugins.always_open_pdf_externally to False.
    2. Add an option named profile.default_content_settings.popups with a value 0.

    The whole option is:

    options = webdriver.ChromeOptions()
    options.add_experimental_option('prefs', {
    "profile.default_content_settings.popups": 0,  # disable the popup window
    "download.default_directory": "file_path", #Change default directory for downloads
    "download.prompt_for_download": False, 
    "download.directory_upgrade": True,
    "plugins.always_open_pdf_externally": False #It will not show PDF directly in chrome 
    })
    wd = webdriver.Chrome(options=options)