相关文章推荐
豁达的上铺  ·  python的dispatch_win32c ...·  2 年前    · 
不羁的高山  ·  spark sql 大小写-掘金·  2 年前    · 

我不知道当我使用ChromeDriver的Selnium自动批量打印这些页面时,要传递什么参数来复制这个设置。

appState = { "recentDestinations": [{
                "id": "Save as PDF",
                "origin": "local",
                "account": "",
                "printing.scaling": 'Custom', # <== Does it go here?
             "selectedDestinationId": "Save as PDF",
             "version": 2,
             "printing.scaling": 'Custom',  # <== Or here?
profile = { 'printing.print_preview_sticky_settings.appState': json.dumps(appState),
            'printing.print_header_footer': False,
            # So many different versions of things I have tried :-(
            'printing.scaling': 'Custom',
            'printing.scaling_type': 'Custom',
            'print_preview.scaling': 'Custom',
            'print_preview.scaling_type': 'Custom',
            'printing.custom_scaling': True,
            'printing.fit_to_page_scaling': 50,
            'printing.page_scaling': True,
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('prefs', profile)
br = webdriver.Chrome(options=chrome_options)

上面显示的所有不同的选项都是在阅读了大量的Chromium源代码试图获得提示后的猜测。

https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc?view=markup https://chromium.googlesource.com/chromium/src/+/master/printing/print_job_constants.cc https://chromium.googlesource.com/chromium/src/+/master/printing/print_job_constants.h

我已经没有线索了......谁有什么想法? 谢谢你们!"。

python
selenium
printing
selenium-chromedriver
chromium
TAH
TAH
发布于 2019-11-20
3 个回答
askvictor
askvictor
发布于 2020-05-08
已采纳
0 人赞同

经过大量的搜索和试错,我终于找到了解决方案这个设置存在于appState字典中,被称为 "scalingType",但它是一个枚举,恼人的是它似乎只接受一个数字,这些数字被定义为here (Github mirror) or here (googlesource).3给你提供了自定义缩放,然后你可以在 "缩放 "设置中定义它(这是一个字符串!)。所以我的设置现在看起来像。

chrome_options = webdriver.ChromeOptions()
appState = {"recentDestinations": [{"id": "Save as PDF", "origin": "local", "account": ""}],
            "selectedDestinationId": "Save as PDF",
            "version": 2,
            "isHeaderFooterEnabled": False,
            "isLandscapeEnabled": True,
            "scalingType": 3,
            "scaling": "141"}
prefs = {'printing.print_preview_sticky_settings.appState': json.dumps(appState),
         'savefile.default_directory': BASE_DIR}
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('kiosk-printing')
driver = webdriver.Chrome(f'{BASE_DIR}\\chromedriver.exe', options=chrome_options)

目前缩放类型的选项是。

  • 0: default
  • 1: fit to page
  • 2: fit to paper
  • 3: custom
  • 但我在 "适合 "选项中没有取得任何成功。

    更为普遍的是,这些设置/选项中的大部分可以通过查看以下内容来确定this file的chromium源代码和/或其包含的文件夹。

    谢谢你的帮助!在scalingType后面有一个错误的方括号。它不允许我只编辑这个。
    puchal
    puchal
    发布于 2020-05-08
    0 人赞同

    我知道这不是你问题的确切答案,但可能是你问题的解决方案。

    你有没有试过不通过偏好,而只是放大页面内容,然后打印页面?

    driver.execute_script("document.body.style.zoom='50%'")
    # continue with printing
    

    对我来说,使用Default的缩放选项进行放大和打印,就像在Scale设置为50的情况下打印页面一样。

    TAH
    这是个好主意,但不幸的是,对这些特定的页面不起作用......它们上面有一堆由D3生成的SVG图表,而缩放设置对它们不起作用。 不过,还是要谢谢你!
    timmyt123
    timmyt123
    发布于 2020-05-08
    0 人赞同

    我最后用阴影根来改变比例。 这就是我所做的。

    driver1.switch_to.window(driver1.window_handles[-2])  # THIS IS THE PRINT DIALOG WINDOW - This could be -1 depending on how many windows are opened
    endTime = time.time() + 10
    while True:
        try:  # PRESS 'More settings' TO EXPAND
            expand = driver1.execute_script(
            "return document.querySelector('print-preview-app').shadowRoot.querySelector('print-preview-sidebar#sidebar').shadowRoot.querySelector('div#container').querySelector('print-preview-more-settings').shadowRoot.querySelector('div').querySelector('cr-expand-button')")
            if expand:
                expand.click()
                break
        except:
        time.sleep(.3)
        if time.time() > endTime:  # passed the waiting period
            driver1.switch_to.window(driver1.window_handles[0])
            break
    try:  # SELECT SCALE METHOD 'CUSTOM'
        scaling = Select(driver1.execute_script(
            "return document.querySelector('print-preview-app').shadowRoot.querySelector('#sidebar').shadowRoot.querySelector('#container').querySelector('#moreSettings').querySelector('print-preview-scaling-settings.settings-section').shadowRoot.querySelector('print-preview-settings-section').querySelector('div').querySelector('select.md-select')"))
        scaling.select_by_value('3')
    except:
        driver1.switch_to.window(driver1.window_handles[0])
    try:  # ENTER SCALE NUMBER
        time.sleep(.3)
        scaling_number = driver1.execute_script(
            "return document.querySelector('print-preview-app').shadowRoot.querySelector('#sidebar').shadowRoot.querySelector('#container').querySelector('#moreSettings').querySelector('print-preview-scaling-settings').shadowRoot.querySelector('iron-collapse').querySelector('print-preview-number-settings-section').shadowRoot.querySelector('print-preview-settings-section').querySelector('#controls').querySelector('span').querySelector('#userValue').shadowRoot.querySelector('#row-container').querySelector('#input-container').querySelector('div#inner-input-container').querySelector('#input')")
        scaling_number.clear()
        scaling_number.send_keys('50')