如何防止硒(Chrome)覆盖现有下载的文件-Python

发布于 2025-02-12 19:51:48 字数 1444 浏览 0 评论 0原文

我正在自动化通过硒下载文件。 当我通过Selenium单击此下载按钮时,它将自动下载Excel文件。 不会打开任何提示,默认文件名是p3mlf.xls或p3mlf(xx).xls,xx =数字,取决于下载的最后一个文件。

在此处输入图像描述

我在正常模式下运行脚本时没有问题(不是无头)。 但是,在无头模式下,下载的文件名始终为p3mlf.xls并覆盖现有的p3mlf.xls文件,而无需添加新文件p3mlf(xx).xls。

我如何防止在无头模式下覆盖现有文件并正常下载。

这是我发现能够以无头模式下载的代码:

  def download(self):
    self.opt = webdriver.ChromeOptions()
    self.opt.add_experimental_option('detach', True)
    self.opt.add_argument('headless')
    self.opt.add_argument('window-size=1920x1080')
    self.opt.add_argument('disable-gpu')
    self.opt.add_argument('--enable-javascript')
    self.ser = Service('SupportingFiles\chromedriver.exe')
    self.driver = webdriver.Chrome(options=self.opt, service=self.ser)
    self.prefs = {
        'profile.default_content_settings.popups': 0,
        'download.default_directory': 'C:\\Drive_D\\Download', #this part actually not wowking
        'download.prompt_for_download': False,
        'download.directory_upgrade': True
        }
    self.opt.add_experimental_option("prefs", self.prefs)
    self.driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
    params = {'cmd':'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath':  self.dlPath_box.get()}}
    self.driver.execute('send_command', params)

I'm automating to download a file through selenium.
When I click this download button through selenium, it will automatically download the excel file.
No prompt will open, and the default file name is P3MLF.xls or P3MLF(xx).xls, xx = number depending on the last file downloaded.

enter image description here

I have no issue when running the script in normal mode (not headless).
But when in headless mode, downloaded file name is always P3MLF.xls and overwrites the existing P3MLF.xls file without adding a new file P3MLF(xx).xls.

How can I prevent to overwrite the existing file and download as normal in headless mode.

Here is my code I found to be able to download in headless mode:

  def download(self):
    self.opt = webdriver.ChromeOptions()
    self.opt.add_experimental_option('detach', True)
    self.opt.add_argument('headless')
    self.opt.add_argument('window-size=1920x1080')
    self.opt.add_argument('disable-gpu')
    self.opt.add_argument('--enable-javascript')
    self.ser = Service('SupportingFiles\chromedriver.exe')
    self.driver = webdriver.Chrome(options=self.opt, service=self.ser)
    self.prefs = {
        'profile.default_content_settings.popups': 0,
        'download.default_directory': 'C:\\Drive_D\\Download', #this part actually not wowking
        'download.prompt_for_download': False,
        'download.directory_upgrade': True
        }
    self.opt.add_experimental_option("prefs", self.prefs)
    self.driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
    params = {'cmd':'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath':  self.dlPath_box.get()}}
    self.driver.execute('send_command', params)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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