The ChromeDriver for Selenium was able to open an embedded PDF after login, but how can I save the PDF file in chrome to local disk? thanks.
Asked
Active
Viewed 6,026 times
3
-
3Welcome to Stackoverflow! You're likely to get a better response to your question if you post some code that shows what you've tried to solve the problem yourself. – NoChinDeluxe Oct 28 '15 at 22:21
-
I managed to achieve the task by disable chrome pdf viewer and let the chrome browser directly download the pdf file – jli Oct 30 '15 at 14:09
-
Possible duplicate of [Chromedriver, Selenium - Automate downloads](https://stackoverflow.com/questions/26894071/chromedriver-selenium-automate-downloads) – Nemo May 07 '18 at 09:52
1 Answers
10
def download_pdf(lnk):
options = webdriver.ChromeOptions()
tgt = tempfile.mkdtemp()
profile = {"plugins.plugins_list": [{"enabled":False,"name":"Chrome PDF Viewer"}],
"download.default_directory" : tgt}
options.add_experimental_option("prefs",profile)
driver = webdriver.Chrome(CHROMEDRIVER, chrome_options = options)
driver.get(lnk)
driver.find_element_by_id('userName1').send_keys('username')
driver.find_element_by_id('password1').send_keys('password')
driver.find_element_by_id('loginButton1').click()
ftgt = os.path.join(tgt,'downloaed.pdf')
while not os.path.exists(ftgt):
time.sleep(3)
driver.close()
return ftgt

jli
- 151
- 2
- 5
-
Why is it necessary to use tempfile? And why to wait for os.path.exists? – B Furtado Feb 18 '19 at 21:51