我无法将 chrome 设置为 PDF 的默认保存选项(python,selenium)

发布于 2025-01-09 08:39:47 字数 1931 浏览 0 评论 0原文

我正在尝试将页面另存为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文