如何使用Selenium&#x2B更改保存的PDF PAGE名称Chromedrive
我有一个脚本,该脚本使用Selenium Python下载PDF页面根据此问题制作
目前我的目标是更改此文件的名称,以便将其定位为我选择的名称,然后更改已保存文件的目标文件夹。
我的疑问是: 我应该在哪里更改,以便使用我选择的名称保存文件?
即使对“ prefs =”变量的更改进行了更改,该文件仍将保存在默认的Chrome目录中。
目前,我有以下代码:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless') # Escondendo o navegador
driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
settings = {
"recentDestinations": [{
"id": "Save as PDF",
"origin": "local",
"account": "",
}],
"isLandscapeEnabled": True,
"selectedDestinationId": "Save as PDF",
"version": 2,
}
prefs = {
"printing.print_preview_sticky_settings.appState": json.dumps(settings),
"profile.default_content_settings.popups" : 0,
"download.name":"name_file", # ?????? ESTE CÓDIGO NÃO ALTERA O NOME
"download.default_directory": r'C:\Users\diretorio_escolhido\\' # ESTE CÓDIGO NÃO ALTERA O DESTINO,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
}
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(name_Object.url)
sleep(5) # Pausa para carregar os dados
driver.execute_script('window.print();')
print('Gerou o PDF')
I have a script that uses Selenium Python to download a PDF page made based on this question
My goal at the moment is to change the name of this file so that it is located with the name I chose and then change the destination folder of the saved file.
My doubts are:
Where should I change so that the file is saved with the name I choose?
Even with the changes made to the "prefs=" variable, the file continues to be saved in the default chrome directory.
At the moment I have the following code:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless') # Escondendo o navegador
driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
settings = {
"recentDestinations": [{
"id": "Save as PDF",
"origin": "local",
"account": "",
}],
"isLandscapeEnabled": True,
"selectedDestinationId": "Save as PDF",
"version": 2,
}
prefs = {
"printing.print_preview_sticky_settings.appState": json.dumps(settings),
"profile.default_content_settings.popups" : 0,
"download.name":"name_file", # ?????? ESTE CÓDIGO NÃO ALTERA O NOME
"download.default_directory": r'C:\Users\diretorio_escolhido\\' # ESTE CÓDIGO NÃO ALTERA O DESTINO,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
}
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(name_Object.url)
sleep(5) # Pausa para carregar os dados
driver.execute_script('window.print();')
print('Gerou o PDF')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我面临同样的问题。目前,我找到了以下解决方法:
I'm facing the same question. For now I found the following workaround:
更改
download.default_directory
tosavefile.default_directory
,因此保存的位置可工作。可悲的是,我认为下载前我们不能更改文件名,但是您可以在下载后重命名您的文件,通过重命名在下载文件夹中文件:
Change
download.default_directory
tosavefile.default_directory
so your saving location works.Sadly I think we can't change the filename before download, but you can rename your file after download, by renaming the latest file in download folder:
此答案使用 vimuth's 上面发布的建议,并且使用python 3.9.4
This answer is using vimuth's suggestion posted above, and it is fully worked for myself using Python 3.9.4