How do you automatically download a Pdf with Selenium Webdriver in Python?

Rest AssuredDynamic ProgrammingProgramming

We can automatically download a pdf with the Selenium webdriver in Python. A file is downloaded in the default path set in the Chrome browser. However, we can modify the path of the downloaded file programmatically in Selenium.

This is done with the help of the Options class. We have to create an object of this class and apply the add_experimental_option. We have to pass the parameters - prefs and the path where the pdf is to be downloaded to this method. Finally, this information has to be sent to the webdriver object.

Syntax

op = Options()
op.add_experimental_option("prefs", p)

Example

Code Implementation

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#Options instance
op = Options()
#send browser option to webdriver object
driver = webdriver.Chrome(executable_path='../drivers/chromedriver', options=op)
#implicit wait
driver.implicitly_wait(0.8)
#url launch
driver.close()