如何用selenium更改chrome的默认下载路径?

时间:2017-02-03 10:17:41

标签: python google-chrome selenium

我的方案是:我必须编写一个脚本来使用Selenium和Python 3.6下载大量文件,现在,我必须使用相同的技术下载文件。

重点是该脚本不会在我自己的计算机上执行。

是否可以使用chrome webdriver获取chrome的默认下载文件夹?

截至目前,我有这段代码:

dlPth="C:\\Users\\genieelecpsim\\Downloads\\"
nwPth="C:\\Users\\genieelecpsim\\Downloads\\Exports"
       for file in os.listdir(dlPth):
         if file.startswith("export") and file.endswith(".csv"):
          print(str(years[i])+"-"+str(months[j])+"-"+str(days[k]))
           newfile=os.path.join(nwPth,str(years[i]) +"-" +(str(months[j]) if months[j]>=10 else "0"+str(months[j]))+"-" +(str(days[k]) if days[k]>=10 else "0"+str(days[k])) +".csv")
            shutil.move(os.path.join(dlPth,file),newfile)
             print (newfile)
              break

我想在这里做的是:

dlPth=# Chrome's default download directory
nwPth=dlPth+"\\Export"

有可能吗?谢谢你的回复!

编辑:首先,感谢所有人的快速回答,似乎我的主题是一个复杂的,但由于我没有使用与this one相同的配置,我想知道这个方法是否适用于py3.6和Selenium 3.0.2 ......对不起,我不能直接评论你的答案,因为我是新来的,但感谢大家!

2 个答案:

答案 0 :(得分:0)

启动驱动程序时,您可以使用特定首选项更改下载文件夹。你应该这样设置:

("download.default_directory", yourWantedPath)

不确定如何启动和配置驱动程序,因此无法为您提供更多代码,但这是您正在寻找的偏好。

你可以找到有用的东西here

答案 1 :(得分:0)

答案并没有真正帮助我,但无论如何,谢谢。我通过使用OS库找到了另一种方法:

# dlPth will be the path to the download directory of the current user (on the system)
dlPth=os.path.join(os.getenv('USERPROFILE'), 'Downloads')

# destPth will just be a directory where I'll put all my (renamed) files in.
destPth=dlPth+"\\Exports\\"

感谢您的回答,我在这里发布了答案,所以任何寻求这方面帮助的人都能看到它

相关问题