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

2024-10-01 17:26:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我的场景是:我必须使用Selenium和Python3.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

我想在这里做的是:

^{pr2}$

有可能吗?感谢您的回复!在

编辑:首先,感谢大家的快速回答,我的主题似乎是重复的,但由于我使用的配置与this one不同,我想知道这种方法是否适用于py3.6和Selenium 3.0.2。。。很抱歉,我不能直接评论你的回答,因为我是新来的,但谢谢大家!在


Tags: 脚本ifosdownloadsseleniumchromedaysusers
2条回答

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

("download.default_directory", yourWantedPath)

不知道你是如何启动和配置你的驱动程序,所以不能帮助你更多的代码,但这是你要找的首选项。在

你可以找到有用的东西here。在

答案对我没什么帮助,但还是要谢谢你。通过使用操作系统库,我找到了另一种方法:

# 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\\"

谢谢你的回答,我把答案贴在这里,这样任何人在这个主题上寻求帮助都可以看到它

相关问题 更多 >

    热门问题