python - 我如何禁用页眉和页脚 Selenium 打印

标签 python selenium

有谁知道如何在使用 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') 行只是为了在屏幕截图中显示取消选择的选项。 enter image description here

您可以在以下页面找到有关 Chrome 选项的详细信息

https://github.com/chromium/chromium/blob/eadef3f685cd9e96e94fcb9645b6838b6d0907a8/chrome/browser/resources/print_preview/data/model.js

enter image description here

关于python - 我如何禁用页眉和页脚 Selenium 打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60609330/

相关文章:

python - 如何在 python 中执行批处理文件后关闭命令提示符窗口?

python - 在 Python 和 matplotlib 中连接 3D 散点图中的两个点

php - Python 相当于 PHP Post

python - 加快 Pandas 中自回归项的创建?

python - 在训练周期的一部分之后运行评估

Java/Selenium RemoteWebDriver/Maven/JUnit - 尝试调用浏览器时获取空 session ID

python - 当选择标签样式属性设置为显示: none; in python selenium时如何从下拉列表中检索值

python-3.x - 即使在 chromedriver 之后,Chrome 也会随机崩溃

java - Selenium Connection Reset : Unable to find an exact match for CDP version 96, 所以返回找到的最接近的版本:95

javascript - Selenium & webdriver.io 如何使用executeScript?