Selenium Chrome另存为pdf更改下载文件夹 [英] Selenium Chrome save as pdf change download folder
问题描述
我想将网站下载为 pdf 文件,它工作正常,但它应该将文件下载到特定路径,而不是将文件下载到我的默认下载目录.
I want to download a website as pdf file, it's working fine, but it should download the file to a specific path, instead it's just downloading the file to my default download directory.
import json
from selenium import webdriver
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local"
}
],
"selectedDestinationId": "Save as PDF",
"version": 2,
'download.default_directory': 'C:\\Users\\Oli\\Google Drive',
"download.directory_upgrade": True
}
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)
driver.get('https://www.google.com/')
driver.execute_script('window.print();')
顺便说一句,有人想用特定名称保护文件吗?
By the way anyone got an idea to safe the file with a specific name?
推荐答案
download.default_directory
设置仅适用于下载的内容.Chrome 以不同的方式处理保存在页面上的文件.要更改页面打印输出的默认文件夹,只需设置 savefile.default_directory
值.
The download.default_directory
setting is only for downloaded content. Chrome treats files saved on the page differently. To change the default folder for a printout of the page, simply set the savefile.default_directory
value instead.
因此,为自定义位置打印为 pdf 的完整示例:
So the full example to print to pdf for a custom location:
import json
from selenium import webdriver
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local",
"account": ""
}
],
"selectedDestinationId": "Save as PDF",
"version": 2
}
profile = {'printing.print_preview_sticky_settings.appState': json.dumps(appState),
'savefile.default_directory': 'path/to/dir/'}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', profile)
chrome_options.add_argument('--kiosk-printing')
driver = webdriver.Chrome(options=chrome_options)
driver.get(url)
driver.execute_script('window.print();')
这篇关于Selenium Chrome另存为pdf更改下载文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!