- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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()
- Related Articles
- How to run Selenium tests on Chrome Browser using?
- How to save figures to pdf as raster images in Matplotlib?
- How to setup Chrome driver with Selenium on MacOS?
- How to save a canvas as PNG in Selenium?
- Using the Selenium WebDriver - Unable to launch chrome browser on Mac
- How to save a plot in pdf in R?
- How to save and load cookies using Python Selenium WebDriver?
- How to launch Chrome Browser via Selenium?
- How to handle chrome notification in Selenium?
- How do I pass options to the Selenium Chrome driver using Python?
- How to extract text from a web page using Selenium and save it as a text file?
- How to open chrome default profile with selenium?
- Set Chrome's language using Selenium ChromeDriver
- How to download all pdf files with selenium python?
- How to run Selenium WebDriver test cases in Chrome?

Advertisements