[英]How to specify print location for PDF printing with Selenium
無論我做什么,這些文件都會一直打印到我的下載(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();')
您有兩個問題,第一個是您設置了兩次首選項,並且由於prefs
add_experimental_option()
使用字典作為選項,因此第二個設置覆蓋了第一個設置,並且所有這些設置實際上都被刪除了。
self._experimental_options = {}
def add_experimental_option(self, name, value):
"""
Adds an experimental option which is passed to chrome.
Args:
name: The experimental option name.
value: The option value.
"""
self._experimental_options[name] = value
第二個問題是在路徑上使用原始字符串r
,它的計算結果為C:\Users\Lucas\Downloads\ECV\\\\
這是無效的。 使用/
而不是\
"C:/Users/Lucas/Downloads/ECV/"
或使用\\
而不使用r
"C:\\Users\\Lucas\\Downloads\\ECV\\"
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),
"download.prompt_for_download": False,
"profile.default_content_setting_values.automatic_downloads": 1,
"download.default_directory": "C:\\Users\\Lucas\\Downloads\\ECV\\",
"savefile.default_directory": "C:\\Users\\Lucas\\Downloads\\ECV\\",
"download.directory_upgrade": True,
"safebrowsing.enabled": True}
options = Options()
options.add_experimental_option('prefs', prefs)
options.add_argument('--kiosk-printing')
driver = webdriver.Chrome(options=options)
driver.get('https://stackoverflow.com/')
driver.execute_script('window.print();')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.