1

I'm working a program that will utilize Selenium/Webdriver to open a webpage, enter some data, and open a new page which is a PDF. Ultimately I would like to download that PDF into a folder. I know it is possible to download a PDF into a folder if you have the URL in your script, but I'm struggling to find a way to download it if it is opened within the program.

A) Is there a way to download a PDF that is opened explicitly in Chrome using a script? B) Is there a way to extract the URL from an opened webpage that then be fed back into the program to download from?

Phil
  • 151
  • 1
  • 8

1 Answers1

1

While I was doing a selenium project, I faced a similar issue.

I would click on a link that was referring to a PDF file but instead of downloading, the selenium chromedriver would just open it in a new tab.

What solved my problem was that right after I started new chromedriver session, I manually disabled this feature:

  1. In your Chrome settings, go to Privacy and Securtiy
  2. Select Site Settings
  3. Scroll down and click on Additional preferences
  4. Find a section named 'PDF documents'
  5. Turn on the option that says "Download PDF files instead of automatically opening them in Chrome"

Now, any PDF link you click on will download the file instead of opening them in a new tab. Note that you need to do this every time you start a new chromedriver. Changing this setting in your main Chrome application won't help.

Bex T.
  • 1,062
  • 1
  • 12
  • 28
  • Thanks for the reply. I think I came across your answer in my SO search actually! Unfortunately I'm running the ChromeDriver headless so I can't manually select anything, everything will need to be done automatically behind the scenes. – Phil Dec 16 '20 at 05:22