gpt4 book ai didi

python - Selenium Chrome 另存为 pdf 更改下载文件夹

转载 作者:行者123 更新时间:2023-12-01 01:13:49 25 4
gpt4 key购买 nike

我想将网站下载为 pdf 文件,它工作正常,但它应该将文件下载到特定路径,而只是将文件下载到我的默认下载目录。

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();')

顺便问一下,有人想出用特定名称来保护文件的想法吗?

最佳答案

download.default_directory 设置仅适用于下载的内容。 Chrome 以不同的方式处理页面上保存的文件。要更改页面打印输出的默认文件夹,只需设置 savefile.default_directory 值即可。

因此,在自定义位置打印为 pdf 的完整示例:

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();')

关于python - Selenium Chrome 另存为 pdf 更改下载文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54578876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com