如何通过Chrome上的Python使用Selenium更改多个下载的文件目录,而不必多次启动Web驱动程序和链接? [英] How to use Selenium via Python on Chrome to change multiple downloaded file directories without having to launch the web driver & link more than once?

查看:46
本文介绍了如何通过Chrome上的Python使用Selenium更改多个下载的文件目录,而不必多次启动Web驱动程序和链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Selenium的新手,已经尝试这样做有一段时间了,我在网上能找到的唯一方法是只在启动Web驱动程序和链接时更改下载文件目录的方法。我主要想做的是从一个Chrome页面下载多个文件,并让Selenium更改每个文件的下载路径目录,而不必每次都重新启动驱动程序和浏览器。如有任何帮助或建议,我们将不胜感激

推荐答案

您可以使用driver.command_executor方法来实现。它允许您与当前浏览器会话交互。 您可以使用此方法更改下载路径,而无需重新启动Web驱动程序。

代码片段如下所示-

您可以根据需要更改'downloadPath'参数。

#initially setting the download path to current directory
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow','downloadPath':os.getcwd()}}
command_result = driver.execute("send_command", params)

#your code to download the file

#followed by changing the download directory
#for example here I'm changing it to data folder inside the current working directory

driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow','downloadPath':os.getcwd()+'data'}}
command_result = driver.execute("send_command", params)

这篇关于如何通过Chrome上的Python使用Selenium更改多个下载的文件目录,而不必多次启动Web驱动程序和链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆