具体操作见代码:
path =CHROME_PATE
out_path = r'D:\005\pdf'
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(
'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36')
# chrome_options.add_argument("disable-blink-features=AutomationControlled") # 就是这一行告诉chrome去掉了webdriver痕迹
#浏览器的设置选项,操作打印机
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local"
}
],
"selectedDestinationId": "Save as PDF",
"version": 2,
}
prefs = {
'printing.print_preview_sticky_settings.appState':json.dumps(appState),
# 'download.default_directory': out_path,
# "download.directory_upgrade": True
}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path=path,
chrome_options=chrome_options,
)
chrome_options.add_argument('--kiosk-printing')
# 设置隐性等待时间最长是30秒钟
driver.implicitly_wait(10)
driver.get(url)
#必须执行此操作
driver.execute_script('window.print();')