首页 > 解决方案 > Chromedriver另存为PDF方法不起作用

问题描述

Chromedriver 另存为 PDF 方法不起作用。在它工作正常之前,但现在每当我运行此代码时,它都会进入另存为 PDF 屏幕并实际打印页面而不是另存为 PDF

我尝试更改ID,但它也没有奏效

appState = {
        "recentDestinations": [
            {
                "id": "Save as PDF",
                "origin": "local",
                "margin": 0,
                'size': 'auto'
            }
        ],
        "selectedDestinationId": "Save as PDF",
        "version": 2,
        "margin": 0,
        'size': 'auto'

    }

profile = {'printing.print_preview_sticky_settings.appState': json.dumps(
        appState)}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', profile)
chrome_options.add_argument('--kiosk-printing')
driver = webdriver.Chrome(chrome_options=chrome_options)

它应该将文件保存为 PDF 而不是选择 Microsoft XPS Document Writer 选项并打印页面 这是它应该选择的选项

标签: python-3.xselenium-webdriverselenium-chromedriver

解决方案


使用 Chrome 版本 78.0.3904.108(官方版本)(64 位)。

添加"account": """recentDestinations".

appState = { "recentDestinations": [{
                  "id": "Save as PDF",
                  "origin": "local",
                  "account": "", # <======= Add this
             }],
             "selectedDestinationId": "Save as PDF",
             "version": 2,
           }

推荐阅读