0

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

1 Answer 1

0

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response but now the entire PDF Viewer is showing as if I implemented no code. @
@AustinSimpson Can you post the url of the pdf file?
Originally no. I did not think I had access to it, but after further digging into the html I was able to find the src link within the embedded Iframe. Now I am able to use driver.get(src) and it is downloaded instantly. Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.