有谁知道如何在硒中打印时禁用“页眉和页脚”选项?默认情况下设置为 true,有人知道如何解决这个问题吗?谢谢你!!!
import json
import os
from selenium import webdriver
# setting html path
htmlPath = os.getcwd() + "\\sample.html"
addr = "file:///" + htmlPath
# setting Chrome Driver
chromeOpt = webdriver.ChromeOptions()
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local",
"account": ""
}
],
"selectedDestinationId": "Save as PDF",
"version": 2
}
prefs = {
'printing.print_preview_sticky_settings.appState': json.dumps(appState)}
chromeOpt.add_experimental_option('prefs', prefs)
chromeOpt.add_argument('--kiosk-printing')
driver = webdriver.Chrome('.\\bin\\chromedriver', options=chromeOpt)
# HTML open and print
driver.get(addr)
driver.execute_script('return window.print()')```
Run Code Online (Sandbox Code Playgroud)
只需添加"isHeaderFooterEnabled": False到您的appstate以下更改即可。
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local",
"account": ""
}
],
"selectedDestinationId": "Save as PDF",
"version": 2,
"isHeaderFooterEnabled": False
}
Run Code Online (Sandbox Code Playgroud)
屏幕截图:注释chromeOpt.add_argument('--kiosk-printing')行只是为了在屏幕截图中显示取消选择的选项。

您可以在下页找到有关 chromium 选项的详细信息
| 归档时间: |
|
| 查看次数: |
3207 次 |
| 最近记录: |