<i id='NCebK'><tr id='NCebK'><dt id='NCebK'><q id='NCebK'><span id='NCebK'><b id='NCebK'><form id='NCebK'><ins id='NCebK'></ins><ul id='NCebK'></ul><sub id='NCebK'></sub></form><legend id='NCebK'></legend><bdo id='NCebK'><pre id='NCebK'><center id='NCebK'></center></pre></bdo></b><th id='NCebK'></th></span></q></dt></tr></i><div id='NCebK'><tfoot id='NCebK'></tfoot><dl id='NCebK'><fieldset id='NCebK'></fieldset></dl></div>

<tfoot id='NCebK'></tfoot>
        • <bdo id='NCebK'></bdo><ul id='NCebK'></ul>
      1. <legend id='NCebK'><style id='NCebK'><dir id='NCebK'><q id='NCebK'></q></dir></style></legend>

      2. <small id='NCebK'></small><noframes id='NCebK'>

        使用 Selenium Webdriver 下载时命名文件

        naming a file when downloading with Selenium Webdriver(使用 Selenium Webdriver 下载时命名文件)
        • <bdo id='Uotw4'></bdo><ul id='Uotw4'></ul>
            <tbody id='Uotw4'></tbody>
              <legend id='Uotw4'><style id='Uotw4'><dir id='Uotw4'><q id='Uotw4'></q></dir></style></legend>

            • <tfoot id='Uotw4'></tfoot>

              <small id='Uotw4'></small><noframes id='Uotw4'>

                <i id='Uotw4'><tr id='Uotw4'><dt id='Uotw4'><q id='Uotw4'><span id='Uotw4'><b id='Uotw4'><form id='Uotw4'><ins id='Uotw4'></ins><ul id='Uotw4'></ul><sub id='Uotw4'></sub></form><legend id='Uotw4'></legend><bdo id='Uotw4'><pre id='Uotw4'><center id='Uotw4'></center></pre></bdo></b><th id='Uotw4'></th></span></q></dt></tr></i><div id='Uotw4'><tfoot id='Uotw4'></tfoot><dl id='Uotw4'><fieldset id='Uotw4'></fieldset></dl></div>

                  本文介绍了使用 Selenium Webdriver 下载时命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我看到你可以通过Webdriver设置下载文件到哪里,如下:

                  I see that you can set where to download a file to through Webdriver, as follows:

                  fp = webdriver.FirefoxProfile()
                  
                  fp.set_preference("browser.download.folderList",2)
                  fp.set_preference("browser.download.manager.showWhenStarting",False)
                  fp.set_preference("browser.download.dir",getcwd())
                  fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")
                  
                  browser = webdriver.Firefox(firefox_profile=fp)
                  

                  但是,我想知道是否有类似的方法可以在下载文件时为其命名?最好不要与配置文件相关联,因为我将通过一个浏览器实例下载大约 6000 个文件,并且不希望每次下载都重新启动驱动程序.

                  But, I was wondering if there is a similar way to give the file a name when it is downloaded? Preferably, probably not something that is associated with the profile, as I will be downloading ~6000 files through one browser instance, and do not want to have to reinitiate the driver for each download.

                  使用所选答案建议的代码的解决方案.每次下载后重命名文件.

                  Solution with code as suggested by the selected answer. Rename the file after each one is downloaded.

                  import os
                  os.chdir(SAVE_TO_DIRECTORY)
                  files = filter(os.path.isfile, os.listdir(SAVE_TO_DIRECTORY))
                  files = [os.path.join(SAVE_TO_DIRECTORY, f) for f in files] # add path to each file
                  files.sort(key=lambda x: os.path.getmtime(x))
                  newest_file = files[-1]
                  os.rename(newest_file, docName+".pdf")
                  

                  推荐答案

                  我不知道是否有一个纯 Selenium 处理程序,但是当我需要对下载的文件做一些事情时,这是我所做的.

                  I do not know if there is a pure Selenium handler for this, but here is what I have done when I needed to do something with the downloaded file.

                  1. 设置一个循环,轮询您的下载目录以获取 具有 .part 扩展名的最新文件(这表示部分下载并且偶尔会出错如果不考虑的话,事情就会发生.在此设置一个计时器,以确保在超时/其他导致下载无法完成的错误的情况下不会进入无限循环.我使用了 ls 的输出Linux 中的 -t <dirname> 命令(我的旧代码使用 commands,已弃用,因此我不会在此处显示:))并通过使用 获取第一个文件p>

                  1. Set a loop that polls your download directory for the latest file that does not have a .part extension (this indicates a partial download and would occasionally trip things up if not accounted for. Put a timer on this to ensure that you don't go into an infinite loop in the case of timeout/other error that causes the download not to complete. I used the output of the ls -t <dirname> command in Linux (my old code uses commands, which is deprecated so I won't show it here :) ) and got the first file by using

                  # result = output of ls -t
                  result = result.split('
                  ')[1].split(' ')[-1]
                  

                1. 如果 while 循环成功退出,目录中最顶层的文件将是您的文件,然后您可以使用 os.rename (或任何其他方式)对其进行修改其他你喜欢的).

                2. If the while loop exits successfully, the topmost file in the directory will be your file, which you can then modify using os.rename (or anything else you like).

                  可能不是您要寻找的答案,但希望它能为您指明正确的方向.

                  Probably not the answer you were looking for, but hopefully it points you in the right direction.

                  这篇关于使用 Selenium Webdriver 下载时命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
                3. 相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                    <tbody id='9NkDx'></tbody>

                  <small id='9NkDx'></small><noframes id='9NkDx'>

                    <bdo id='9NkDx'></bdo><ul id='9NkDx'></ul>
                    <legend id='9NkDx'><style id='9NkDx'><dir id='9NkDx'><q id='9NkDx'></q></dir></style></legend>
                  • <tfoot id='9NkDx'></tfoot>
                    <i id='9NkDx'><tr id='9NkDx'><dt id='9NkDx'><q id='9NkDx'><span id='9NkDx'><b id='9NkDx'><form id='9NkDx'><ins id='9NkDx'></ins><ul id='9NkDx'></ul><sub id='9NkDx'></sub></form><legend id='9NkDx'></legend><bdo id='9NkDx'><pre id='9NkDx'><center id='9NkDx'></center></pre></bdo></b><th id='9NkDx'></th></span></q></dt></tr></i><div id='9NkDx'><tfoot id='9NkDx'></tfoot><dl id='9NkDx'><fieldset id='9NkDx'></fieldset></dl></div>