无论我做什么,文件都会被打印到我的下载文件夹(Windows 默认文件夹)中,而不是指定的文件夹。我进行了研究,显然应该使用
savefile.default_directory
选项而不是 download.default_directory
,但它并没有起作用。我尝试删除路径末尾的 \\
,但没有成功。这是在工作电脑上,如果有任何区别,那就是一台运行 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();')
C:/Users/Lucas/Downloads/ECV/
和C:\\Users\\Lucas\\Downloads\\ECV\\
? - Guyr"C:/Users/Lucas/Downloads/ECV/"
和C:\\Users\\Lucas\\Downloads\\ECV\\
两种解决方案,但都在打印对话框中等待我的输入。 - LucasSeverynoptions.add_argument('--kiosk-printing')
,我已经编辑了答案。 - Guy