【发布时间】:2022-07-08 00:14:41
【问题描述】:
无论我做什么,文件都会一直打印到我的下载(Windows 默认)文件夹中,而不是指定的文件夹中。我做了我的研究,显然应该使用savefile.default_directory 选项而不是download.default_directory 但它无论如何都不起作用。我尝试从路径中删除尾随\\,但没有成功。这是在工作 PC 上,如果有任何区别的话,Windows 10 机器。
import os
os.environ["PATH"] += os.pathsep + r'C:\Program Files (x86)\Chromedriver99';
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
options = Options()
options.add_experimental_option(
"prefs",
{
"download.prompt_for_download": False,
"profile.default_content_setting_values.automatic_downloads": 1,
"download.default_directory": r"C:\Users\Lucas\Downloads\ECV\\",
"savefile.default_directory": r"C:\Users\Lucas\Downloads\ECV\\",
"download.directory_upgrade": True,
"safebrowsing.enabled": True # Some answers include this, makes no difference
},
)
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
# PDF printing settings
print_settings = {
"recentDestinations": [{
"id": "Save as PDF",
"origin": "local",
"account": "",
}],
"selectedDestinationId": "Save as PDF",
"version": 2,
"isHeaderFooterEnabled": False,
"isLandscapeEnabled": True
}
prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(print_settings)}
options.add_experimental_option('prefs', prefs)
options.add_argument('--kiosk-printing') # Some answers include this, makes no difference
driver = webdriver.Chrome(options=options)
driver.get('https://stackoverflow.com/')
driver.execute_script('window.print();')
【问题讨论】:
标签: python selenium selenium-webdriver selenium-chromedriver