Python 使用Selenium和ChromeDriver,自动调整打印页面的大小
我正在写一个脚本,用Chrome自动打印一组网页。如果要手动打印,我会从“比例”下拉列表中选择“自定义”,并在下面的输入字段中输入50 当我使用Selnium with ChromeDriver自动批量打印这些页面时,我无法确定要传递哪些参数来复制此设置Python 使用Selenium和ChromeDriver,自动调整打印页面的大小,python,selenium,printing,selenium-chromedriver,chromium,Python,Selenium,Printing,Selenium Chromedriver,Chromium,我正在写一个脚本,用Chrome自动打印一组网页。如果要手动打印,我会从“比例”下拉列表中选择“自定义”,并在下面的输入字段中输入50 当我使用Selnium with ChromeDriver自动批量打印这些页面时,我无法确定要传递哪些参数来复制此设置 appState = { "recentDestinations": [{ "id": "Save as PDF", "origin": "local",
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)
appState={“recentDestinations”:[{
“id”:“另存为PDF”,
“原产地”:“本地”,
“账户”:“账户”,
“printing.scaling”:“Custom”,我知道这并不能完全回答您的问题,但可能是您问题的解决方案
您是否尝试过不传递首选项,而只是缩小页面内容,然后打印页面
driver.execute_脚本(“document.body.style.zoom='50%”)
#继续打印
对于我来说,使用Default
scale选项缩小和打印就像打印页面时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
经过多次搜索和反复尝试,我终于找到了解决方案!该设置位于appState字典中,名为“scalingType”,但它是一个枚举,令人恼火的是,它似乎只接受一个数字,该数字已定义或.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:海关
但我在“适合”这两个选项中都没有取得任何成功
一般来说,大多数设置/选项都可以通过查看chromium源和/或其包含的文件夹来确定。这是一个好主意,但不幸的是不适用于这些特定页面……它们上面有一堆由D3生成的SVG图表,缩放设置对它们不起作用。不过,谢谢!