Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to save as PDF on Chrome using Selenium
We can save a pdf file on Chrome using the Selenium webdriver. To download the pdf file in a specific location we have to take the help of the Options class.
We shall create an object of this class and apply add_experimental_option on it. Then pass the values - prefs and the path where the pdf is to be downloaded as parameters to this method.
Syntax
o = Options()
o.add_experimental_option("prefs" ,
{"download.default_directory": "../downloads"} )
Example
Code Implementation
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
#object of Options
o = Options()
#path of downloaded pdf
o.add_experimental_option("prefs",{"download.default_directory": "../downloads"})
#pass Option to driver
driver = webdriver.Chrome(executable_path='../drivers/chromedriver', options=o)
#implicit wait
driver.implicitly_wait(0.5)
#url launch
driver.get("http://demo.automationtesting.in/FileDownload.html")
#maximize browser
driver.maximize_window()
#identify elements
l = driver.find_element_by_id('pdfbox')
l.send_keys("test")
m = driver.find_element_by_id('createPdf')
m.click()
n = driver.find_element_by_id('pdf-link-to-download')
n.click()
#driver quit
driver.quit()Advertisements