【发布时间】:2021-04-10 13:16:32
【问题描述】:
我有一个使用 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>
【问题讨论】:
-
你好 Zubayer,你有没有浏览过这个 URL - stackoverflow.com/questions/46077392/…
标签: python python-3.x selenium selenium-webdriver selenium-chromedriver