如何通过 Chrome 上的 Python 使用 Selenium 来更改多个下载的文件目录,而无需多次启动网络驱动程序和 link?
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?
我是 selenium 的新手,已经尝试了一段时间,我在网上唯一能找到的方法是只在启动网络驱动程序时更改下载文件目录和 link.我基本上想做的是从一个 chrome 页面下载多个文件,并让 selenium 更改每个文件的下载路径目录,而不必每次都重新启动驱动程序和浏览器。非常感谢提供的任何帮助或建议
您可以使用driver.command_executor
方法来实现。它允许您与当前浏览器会话进行交互。
您可以使用此方法更改下载路径,而无需重新启动网络驱动程序。
代码片段如下-
您可以根据需要更改 '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)
我是 selenium 的新手,已经尝试了一段时间,我在网上唯一能找到的方法是只在启动网络驱动程序时更改下载文件目录和 link.我基本上想做的是从一个 chrome 页面下载多个文件,并让 selenium 更改每个文件的下载路径目录,而不必每次都重新启动驱动程序和浏览器。非常感谢提供的任何帮助或建议
您可以使用driver.command_executor
方法来实现。它允许您与当前浏览器会话进行交互。
您可以使用此方法更改下载路径,而无需重新启动网络驱动程序。
代码片段如下-
您可以根据需要更改 '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)