使用 Selenium 和 ChromeDriver,自动缩放打印页面的大小
Using Selenium and ChromeDriver, automatically scale the size of the page on print
我正在编写一个脚本来自动打印 Chrome 中的一组网页。如果我要手动打印它们,我会从比例下拉列表中选择 "Custom",然后在下面的输入字段中输入 50。
当我使用带 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
我没有线索了...有人有什么想法吗?谢谢!
我知道这不是您问题的确切答案,但可能是您问题的解决方案。
您是否尝试过不传递首选项,而是缩小页面内容然后打印页面?
driver.execute_script("document.body.style.zoom='50%'")
# continue with printing
对我来说,使用 Default
比例选项缩小和打印就像打印页面 Scale
设置为 50
我最后使用 Shadow Root 来改变比例。
这就是我所做的。
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:
pass
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')
except:
driver1.switch_to.window(driver1.window_handles[0])
driver1.switch_to.window(driver1.window_handles[0]) # back to main window
经过大量搜索和 trial-and-error,我终于找到了解决方案!该设置位于 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:默认
- 1: 适合页面
- 2:适合纸张
- 3: 自定义
但我在 'fit to' 选项中都没有取得任何成功。
更一般地说,大多数 settings/options 可以通过查看 this file 铬源 and/or 它包含的文件夹来确定。
我正在编写一个脚本来自动打印 Chrome 中的一组网页。如果我要手动打印它们,我会从比例下拉列表中选择 "Custom",然后在下面的输入字段中输入 50。
当我使用带 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
我没有线索了...有人有什么想法吗?谢谢!
我知道这不是您问题的确切答案,但可能是您问题的解决方案。
您是否尝试过不传递首选项,而是缩小页面内容然后打印页面?
driver.execute_script("document.body.style.zoom='50%'")
# continue with printing
对我来说,使用 Default
比例选项缩小和打印就像打印页面 Scale
设置为 50
我最后使用 Shadow Root 来改变比例。 这就是我所做的。
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:
pass
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')
except:
driver1.switch_to.window(driver1.window_handles[0])
driver1.switch_to.window(driver1.window_handles[0]) # back to main window
经过大量搜索和 trial-and-error,我终于找到了解决方案!该设置位于 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:默认
- 1: 适合页面
- 2:适合纸张
- 3: 自定义
但我在 'fit to' 选项中都没有取得任何成功。
更一般地说,大多数 settings/options 可以通过查看 this file 铬源 and/or 它包含的文件夹来确定。