有谁知道如何在使用 selenium 打印时禁用“页眉和页脚”选项?它默认设置为 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()')```
最佳答案
只需将 "isHeaderFooterEnabled": False
添加到您的 appstate
下面的更改。
appState = {
"recentDestinations": [
{
"id": "Save as PDF",
"origin": "local",
"account": ""
}
],
"selectedDestinationId": "Save as PDF",
"version": 2,
"isHeaderFooterEnabled": False
}
屏幕截图:注释 chromeOpt.add_argument('--kiosk-printing')
行只是为了在屏幕截图中显示取消选择的选项。
您可以在以下页面找到有关 Chrome 选项的详细信息
关于python - 我如何禁用页眉和页脚 Selenium 打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60609330/