Set Selenium ChromeDriver UserPreferences to Save

2019-01-26 00:21发布

I am using ChromeDriver 2.33 and am using kiosk printing to automatically click the Print button on the Print Preview dialog however it is sending the document to the printer instead of PDF.

I have attempted the solution at this stack overflow question to no avail.

Here is the code I am using:

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);

Can anyone tell me how I set the printer to PDF from the actual printer?

1条回答
Root(大扎)
2楼-- · 2019-01-26 01:26

try adding Save as PDF on 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)
查看更多
登录 后发表回答