我有一个使用 selenium 将 html 转换为 pdf 的脚本(尝试了所有其他转换方法,但 html 文件有点复杂。所以没有其他方法有效)。它工作得很好。但问题是每次我想打印页面时它都会打开一个新窗口。我希望它以最小化的方式打开。如果有人知道怎么做,我将不胜感激。(在无头模式下打印 pdf 不起作用)
我的代码:
from selenium import webdriver
options = webdriver.ChromeOptions()
settings = {
"recentDestinations": [{
"id": "Save as PDF",
"origin": "local",
"account": ""
}],
"selectedDestinationId": "Save as PDF",
"version": 2,
"isHeaderFooterEnabled": False,
"isCssBackgroundEnabled": True
}
prefs = {
'printing.print_preview_sticky_settings.appState': json.dumps(settings),
'savefile.default_directory': "./"
}
options.add_experimental_option('prefs', prefs)
options.add_argument('--kiosk-printing')
options.add_argument('--enable-print-browser')
options.add_argument("--disable-popup-blocking")
options.add_argument('--disable-extensions')
driver = webdriver.Chrome('chromedriver', options=options)
full_path = "file:///home/zubayer/test.html"
driver.get(full_path)
time.sleep(2)
driver.execute_script('window.print();')
driver.quit()
示例 html 文件:
<!DOCTYPE html>
<html>
</head>
<title>Test</test>
</head>
<body>
<h1>This is a test</h1>
<p>Help me</p>
</body>
</html>
答案 0 :(得分:0)
有一种最小化窗口的方法。将此行添加到您的代码中。
driver.minimize_window()