
In the above code I’ve imported the os module and added a new variable path_loc to set where the downloaded PDF’s should be stored. Path_loc = os.path.join(os.getcwd(), "temp") # set location using os.path.join or set it manually if needed.

If you want to set the location where the PDF files will be downloaded you will need to add one more property to the chrome_prefs variable, as seen here: from selenium.webdriver import Chrome, ChromeOptions Set Folder Location Of Downloaded PDF Files The last line then sets these properties using the. Next I create a dictionary of key: value pairs which will help to define the behavior of how to handle PDF’s should a link open a file up.

Options.add_experimental_option("prefs", chrome_prefs)Īs you can see from the above code the options variable creates a new instance of the ChromeOptions() class. "download.open_pdf_in_system_reader": False,

"plugins.always_open_pdf_externally": True, Next is to be able to create a new instance of the ChromeOptions class followed by creating a dictionary of the variables that need to be changed within: from selenium.webdriver import Chrome, ChromeOptions With this import statement I am importing both the Chrome driver (the browser) and ChromeOptions ( Options class). To enable editing the Chrome browser’s preferences require the import of the ChromeOptions class: from selenium.webdriver import Chrome, ChromeOptions The first thing you need to do is to change the default behavior of your Chrome browser so that it doesn’t automatically open the downloaded PDF in the browser window. Here are the essential items you need to configure your Selenium instance and Chrome browser to do the same.

Once you’ve disabled the ability of Chrome to display and open the PDF file within its browser the last setting you would need to change is the location for where you want the file to be stored. The trick to be able to download a PDF file using Selenium without the Chrome browser opening the PDF file within the browser window is to set the preferences of the browser to simply not open PDF’s automatically. How do you download a PDF file when the URL opens up a PDF in your Chrome browser in Python without needing to print the page or use special key presses? And how can you set the location of the PDF?
