I've been using a recipe that I've cobbled together from online snippets in order to get Selenium to print the currently-visited webpage:
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local",
"account": ""
}
],
"selectedDestinationId": "Save as PDF",
"version": 2,
"isCssBackgroundEnabled": True
}
downloadPath=r'C:\temp'
profile = {'printing.print_preview_sticky_settings.appState':json.dumps(appState),
'savefile.default_directory':downloadPath}
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', profile)
options.add_argument('--kiosk-printing')
options.add_argument('--enable-print-browser')
What I am struggling with now is to figure out a way to print only selected pages (e.g., only the first page). I know I can brute-force this with a PDF editor such as How to delete pages from pdf file using Python? but I was curious if anyone knew of a more elegant approach here.
Thank you very much.
Related
Could anyone tell me if there is any option to save pdf file with not default name? In my code everything works fine, the only thing i would like to change is a name of file. Maybe i should add something in setting or prefs? I would like the name to be the same what user input at the begining of the script. Thats the part of my code. Thanks for help in advance.
isin = input('Please provide ISIN: ')
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
settings = {
"recentDestinations": [{
"id": "Save as PDF",
"origin": "local",
"account": "",
}],
"selectedDestinationId": "Save as PDF",
"version": 2
}
prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(settings),
'savefile.default_directory': 'PATH'}
options.add_experimental_option('prefs', prefs)
options.add_argument('--kiosk-printing')
I am using this answer to download and save webpages as a pdf.
Although the method works, it is saving the output files in a different directory.
What could I add to the code to specify a custom output directory?
In addition to that, is there a way to change the output file name?
This is the code so far:
import json
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
settings = {"recentDestinations": [{"id": "Save as PDF", "origin": "local", "account": ""}], "selectedDestinationId": "Save as PDF", "version": 2}
prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(settings)}
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')
You can provide default directory in your preferences by adding 'savefile.default_directory' with a path to your folder, like this:
prefs = {
'printing.print_preview_sticky_settings.appState': json.dumps(settings),
'savefile.default_directory': '/path_to_folder/'
}
(Also, the answer here has an example of how the path should look for Windows)
As far, as saving the file with a custom name, I couldn't find an easy solution myself, but I used this example from SO when I was building something similar. I hope this helps. Good luck!
This is how you can specify your download directory.This is Java solution.
downloadPath=System.getProperty("user.dir")+"\\tempDownloadedFiles";
I try to set experimental_option on ChromeDriver by Selenoid UI. I tried a lot of cases but neither works, usually, I see a normal browser without mobile emulation in local Selenoid UI or browser is set as unknown when I try another way and I don't have a capabilities dictionary.
My code now, which doesn't work correctly:
capabilities = {
"browserName": "chrome",
"version": "80.0",
"enableVNC": True,
"enableVideo": video,
'screenResolution': "1920x1080",
"chromeOptions": {
"mobileEmulation": {
"deviceName": "Nexus 5"
}
}
}
context.browser = webdriver.Remote(
command_executor="http://selenoid:4444/wd/hub",
desired_capabilities=capabilities)
Maybe somebody knows how to set experimental options in the chrome image in Selenoid UI?
I'm trying to save some web pages to PDF using Python, Selenium, and Chrome, and I can't get the printer to default to Chrome's built-in "save as PDF" option.
I have found examples of how to do this in various places online, including in questions people have asked on Stack Overflow, but they way they're all implementing it doesn't work and I'm not sure if something has changed in more recent versions of Chrome, or if I'm somehow doing something wrong (for example, here is a page that has these settings: Missing elements when using selenium chrome driver to automatically 'Save as PDF').
I only included the default download location change in this code to verify it's accepting any changes at all - if you download one of the Python installs from that page, it will download to the new location and not to the standard download folder, so Chrome seems to be accepting these changes.
The problem appears to be the option "selectedDestinationID", which doesn't seem to do anything.
from selenium import webdriver
import time
import json
chrome_options = webdriver.ChromeOptions()
app_state = {
'recentDestinations': [{
'id': 'Save as PDF',
'origin': 'local'
}],
'selectedDestinationId': 'Save as PDF',
'version': 2
}
prefs = {
'printing.print_preview_sticky_settings.appState': json.dumps(app_state),
'download.default_directory': 'c:\\temp\\seleniumtesting\\'
}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path='C:\\temp\\seleniumtesting\\chromedriver.exe', options=chrome_options)
driver.get('https://www.python.org/downloads/release/python-373/')
time.sleep(25)
driver.close()
After the page launches, hitting ctrl+p brings up the printing page, but it defaults to the default printer. If I bring up the same page in my standard Chrome installation, it defaults to printing to PDF. I want to get to the point where I can add kiosk printing and then call window.print(), but as of now all that does is send it to the actual paper printer.
Thanks for any help anyone can offer. I'm stumped, and at this point it probably would have been faster to just save all of these manually.
It seems that if you have network printers configured they load up after opening the dialog and override your selectedDestination.
There is a preference "printing.default_destination_selection_rules" which seems to resolve.
prefs = {
"printing.print_preview_sticky_settings.appState": json.dumps(app_state),
"download.default_directory": "c:\\temp\\seleniumtesting\\".startswith(),
"printing.default_destination_selection_rules": {
"kind": "local",
"namePattern": "Save as PDF",
},
}
https://chromium.googlesource.com/chromium/src/+/master/chrome/common/pref_names.cc#1318
https://www.chromium.org/administrators/policy-list-3#DefaultPrinterSelection
I am trying to create a script to automatically save read-only pdfs via Chrome's printing functionality to save it as another pdf in the same folder. This removes the 'read-only' feature. However while running the script I am not sure where I can specify my own specific destination folder and the script saves it in the Downloads folder directly.
Full props to https://stackoverflow.com/users/1432614/ross-smith-ii for the code below.
Any help will be very much appreciated.
import json
from selenium import webdriver
downloadPath = r'mypath\downloadPdf\\'
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local"
}
],
"selectedDestinationId": "Save as PDF",
"version": 2
}
profile = {'printing.print_preview_sticky_settings.appState':json.dumps(appState)}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', profile)
chrome_options.add_argument('--kiosk-printing')
driver = webdriver.Chrome(chrome_options=chrome_options)
pdfPath = r'mypath\protected.pdf'
driver.get(pdfPath)
driver.execute_script('window.print();')
Ok, I think I figured out the solution. Just append the following line with the below code:
profile = {'printing.print_preview_sticky_settings.appState':json.dumps(appState),'savefile.default_directory':downloadPath}
It's not ideal still as you cannot specify the new file name you want but it works for now.
If anyone has a better solution, please do post it here. Thanks