使用Selenium Webdriver下载文件时如何命名文件

13

我看到您可以通过Webdriver设置文件下载的位置,具体方法如下:

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个文件,并且不想为每个下载重新初始化驱动程序。

3个回答

4
我建议一种有些奇怪的方式:如果可能的话,请不要使用Selenium下载文件。我的意思是获取文件URL并使用urllib库以“手动”的方式下载文件并将其保存到磁盘上。问题在于Selenium没有处理Windows对话框(例如“另存为”对话框)的工具。我不确定,但我怀疑它根本不能处理任何操作系统对话框,请纠正我如果我错了。 :)

以下是一个简单的示例:
import urllib
urllib.urlretrieve( "http://www.yourhost.com/yourfile.ext", "your-file-name.ext")

我们唯一的任务是确保处理所有urllib异常。请参见http://docs.python.org/2/library/urllib.html#urllib.urlretrieve获取更多信息。


谢谢您的建议,但我需要使用Selenium(或类似工具),因为我必须先浏览一些非RESTful链接。 - user1253952
我明白了,你可以在测试用例中轻松使用urllib函数,不是吗? - Pavel Daynyak
我不确定,你能给我一个例子吗?我的目标是下载大约6000个文件(先登录,浏览几页,然后点击下载和下一页按钮6000次)。所以我应该获取下载链接的URL并使用urllib进行操作吗?我认为这样行不通,因为我必须先登录,而且链接不是restful的,所以我认为还需要会话变量。但是,我会尝试一下!如果你有其他建议,请告诉我。 - user1253952
你可以通过调用以下代码获取cookies:cookies = driver.get_cookies()你不能使用urllib,但是urllib2的openers可以帮助你(urllib不知道如何发送cookie头,而urllib2可以):opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(cookielib.CookieJar()) ) opener.addheaders.append( ('Cookies', 'key=val, key2=val2') )此外,你可以构建一个Request对象,并使用Request.add_header将原始标头添加到HTTP请求中(请检查cookie标头格式):Request.add_header('Cookies', 'key=val,key2=val2')如果有不清楚的地方,请告诉我。 - Pavel Daynyak
为什么不使用Selenium来获取最终链接,然后使用Selenium获取链接href并将其传递给urllib来实际检索文件呢? - sadmicrowave

3

我不知道是否存在一个纯Selenium处理程序来解决这个问题,但当我需要对下载的文件进行操作时,下面是我的做法。

  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('\n')[1].split(' ')[-1]
    
  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).

也许这不是你想要的答案,但希望它能指引你走向正确的方向。


谢谢,我想我会选择类似这样的东西。将首选文件名保存到文件中,然后在下载所有文件后列出它们的创建日期,并在那时重命名。 - user1253952
@user1253952 如果这种方法更好的话,您实际上可以在下载时进行更改。很乐意提供帮助。 - RocketDonkey
+1,这就是我为解决这个问题所做的,基本上是不断地轮询下载目录。 - Arran
@Arran Ha,很高兴听到还有其他人也这样做 :) - RocketDonkey

1

根据所选答案的建议,提供带有代码的解决方案。每次下载后重命名文件。

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")

这个答案是由OP user1253952 发布的编辑,遵循CC BY-SA 3.0,在问题Selenium Webdriver下载文件时命名文件下。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接