将Selenium ChromeDriver UserPreferences设置为另存为PDF

时间:2017-10-30 03:23:06

标签: selenium selenium-webdriver selenium-chromedriver

我正在使用ChromeDriver 2.33并使用自助服务终端打印自动点击“打印预览”对话框中的“打印”按钮,但它将文档发送到打印机而不是PDF。

我尝试this stack overflow question的解决方案无济于事。

以下是我正在使用的代码:

ChromeOptions o = new ChromeOptions();
o.AddArgument("--kiosk-printing");
o.AddUserProfilePreference("printing.print_preview_sticky_settings.appState", "{\"version\":2,\"isGcpPromoDismissed\":false,\"selectedDestinationId\":\"Save as PDF\"");
chrome = new ChromeDriver(dir, o);

有谁能告诉我如何从实际打印机将打印机设置为PDF?

1 个答案:

答案 0 :(得分:2)

尝试在Save as PDF上添加recentDestinations

import json
settings = {
    "appState": {
        "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local"
        }],
        "selectedDestinationId": "Save as PDF",
        "version": 2
    }  
}
prefs = {'printing.print_preview_sticky_settings': json.dumps(settings)}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')

driver = webdriver.Chrome(chrome_options=chrome_options)