回复:使用 Selenium (Chrome) 打印:选择页面

Re: Printing with Selenium (Chrome): Select Pages

提问人:Stoufa 提问时间:3/31/2023 最后编辑:Stoufa 更新时间:3/31/2023 访问量:94

问:

我面临着这个问题中解释的相同问题。
使用 Selenium,如何将页面设置为“自定义”并在打印视图中设置其下方的特定页面?

enter image description here

作为参考,这是我的脚本目前的样子:

from selenium import webdriver

import os
import json

settings = {
    "recentDestinations": [{
        "id": "Save as PDF",
        "origin": "local",
        "account": ""
    }],
    "selectedDestinationId": "Save as PDF",
    "version": 2,
    "isHeaderFooterEnabled": False,
    "mediaSize": {
        "height_microns": 210000,
        "name": "ISO_A5",
        "width_microns": 148000,
        "custom_display_name": "A5"
    },
    "customMargins": {},
    "marginsType": 1,  # i.e. None
    "scaling": 100,
    "scalingType": 3,
    "scalingTypePdf": 3,
    "isCssBackgroundEnabled": True,
    # TODO: print a specific range, e.g. 1, 3-4
}

prefs = {
    'printing.print_preview_sticky_settings.appState': json.dumps(settings)
}

chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')
browser = webdriver.Chrome('/path/to/chromedriver', options=chrome_options)

source = os.path.abspath('index.html')
source = f'file:///{source}'

browser.get(source)
browser.execute_script('window.print();')
browser.close()
python google-chrome selenium-chromedriver chromium

评论


答: 暂无答案