我无法将 chrome 设置为 PDF 的默认保存选项(python,selenium)
我正在尝试将页面另存为 PDF,但无法将“id”设置为“另存为 PDF”(chrome 驱动程序版本为 98)。关于堆栈溢出有很多类似的例子,但这些解决方案不起作用。 我可以设置download.dfrault_directory,没关系,只有“另存为PDF”困扰我。 这是我想要控制或应用设置的窗口 我已经提到过这个 -> https://chromium.googlesource。 com/chromium/src/+/refs/heads/main/chrome/common/pref_names.cc https://stackoverflow.com/a/59953341/18220200
这是我的代码。我找不到错误的地方,希望得到任何建议。
#Chromedriver pdf print setting
chromeoptions = webdriver.ChromeOptions()
chromeSettings = {"recentDestinations": [{
"id" : "Save as PDF",
"origin" : "local",
"account" : ""}],
"selectedDestinationID" : "Save as PDF",
"version" : 2,
}
chromeoptions.add_argument('--enable-print-browser')
chromeoptions.add_argument("--safebrowsing-disable-download-protection")
chromeoptions.add_argument("safebrowsing-disable-extension-blacklist")
prefs = {'printing.print_preview_sticky_settings.appState' : json.dumps(chromeSettings)}
chromeoptions.add_experimental_option('prefs', prefs)
chromeoptions.add_experimental_option("prefs", {
"download.default_directory": r"filepath",
'behavior': 'allow',
"download.directory_upgrade": True,
'download.prompt_for_download': False,
'download.extensions_to_open': 'pdf:xls:xml',
'safebrowsing.enabled': True,
'popups' : False
})
+我添加了我的代码“打印部分”
clickByXpath('//print_button')
# Open chrome print window
time.sleep(1)
pyautogui.press('enter')
filename = '\Report' + str(filename+1)
time.sleep(7) #wait for 'save as' window
pyautogui.typewrite(constants.FILE_DOWNLOAD_LOCATION + filename)
pyautogui.press('enter')
time.sleep(1)
driver.switch_to.window(now_window)
pyautogui.hotkey('ctrl', 'w')
I'm trying to save pages as PDF, but cannot set "id" to "Save as PDF"(chrome driver's version is 98). There are many similar examples on stack overflow, but that solutions didn't work.
I can set download.dfrault_directory, it doesn't matter, only "Save as PDF" bothering me.
this is window what i want to control or apply settings
I already reffered this ->
https://chromium.googlesource.com/chromium/src/+/refs/heads/main/chrome/common/pref_names.cc
https://stackoverflow.com/a/59953341/18220200
this is my code. I can't find wrong points, would appreciate any advice.
#Chromedriver pdf print setting
chromeoptions = webdriver.ChromeOptions()
chromeSettings = {"recentDestinations": [{
"id" : "Save as PDF",
"origin" : "local",
"account" : ""}],
"selectedDestinationID" : "Save as PDF",
"version" : 2,
}
chromeoptions.add_argument('--enable-print-browser')
chromeoptions.add_argument("--safebrowsing-disable-download-protection")
chromeoptions.add_argument("safebrowsing-disable-extension-blacklist")
prefs = {'printing.print_preview_sticky_settings.appState' : json.dumps(chromeSettings)}
chromeoptions.add_experimental_option('prefs', prefs)
chromeoptions.add_experimental_option("prefs", {
"download.default_directory": r"filepath",
'behavior': 'allow',
"download.directory_upgrade": True,
'download.prompt_for_download': False,
'download.extensions_to_open': 'pdf:xls:xml',
'safebrowsing.enabled': True,
'popups' : False
})
+I added my codes, 'print part'
clickByXpath('//print_button')
# Open chrome print window
time.sleep(1)
pyautogui.press('enter')
filename = '\Report' + str(filename+1)
time.sleep(7) #wait for 'save as' window
pyautogui.typewrite(constants.FILE_DOWNLOAD_LOCATION + filename)
pyautogui.press('enter')
time.sleep(1)
driver.switch_to.window(now_window)
pyautogui.hotkey('ctrl', 'w')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论