[英]Failed - Download Error when downloading in Selenium headless Chrome
I am trying to download files in Chrome with Selenium. 我正在尝试使用Selenium在Chrome中下载文件。 I discovered that headless Chrome does not allow file downloads by default, and applied a workaround .
我发现无头Chrome默认情况下不允许文件下载,因此应用了一种解决方法 。 However, implementing the workaround caused some files to produce a
Failed - Download Error
in Chrome. 但是,实施替代方法会导致某些文件在Chrome中产生“
Failed - Download Error
。
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': abs_path}}
driver.execute('send_command', params)
Here is what my code looks like: 这是我的代码:
chrome_options = webdriver.ChromeOptions()
prefs = {
"download.prompt_for_download": False, # allow automatic downloads
"plugins.always_open_pdf_externally": True, # allow download of pdf instead of open in plugin
"download.default_directory": path,
"safebrowsing.enabled": False # allow download of .msi, .exe files, etc.
}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': path}}
driver.execute('send_command', params)
for url in file_urls: # file_urls here is a list of download links
driver.get(url)
After searching for common reasons for Download error
, things I have ruled out are: 在搜索“
Download error
常见原因之后,我排除了以下内容:
After removing the workaround, all files are able to download as per normal, but I would then be unable to download in headless mode. 删除解决方法后,所有文件都可以正常下载,但是我将无法以无头模式下载。 Any suggestions would be helpful.
任何的意见都将会有帮助。
Additional information: 附加信息:
ChromeDriver version: 2.40.565498
ChromeDriver版本:
2.40.565498
Chrome version: 67.0.3396.87
Chrome版本:
67.0.3396.87
you can try disabling security , maybe it will work 您可以尝试禁用安全性 ,也许它会起作用
what i would recommend is not to run in headless mode, we run selenium in Linux servers too, and we choose to use separate selenium docker containers (webserver) and create remote drivers instead of local ones: 我建议不要在无头模式下运行,我们也在Linux服务器上运行selenium,并且我们选择使用单独的selenium docker容器(Web服务器)并创建远程驱动程序,而不是本地驱动程序:
check this page for more information 检查此页面以获取更多信息
after you install docker you can launch selenium using this command 安装docker之后,您可以使用以下命令启动selenium
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm --network host selenium/standalone-chrome:3.141.59-neon
make sure to have --network host
so that the 4444
port can be accessed from localhost
. 确保具有
--network host
以便可以从localhost
访问4444
端口。 after that, you can simply create a remote driver by doing this 之后,您只需执行以下操作即可创建一个远程驱动程序
from selenium import webdriver
driver = webdriver.Remote(command_executor = exec_path or 'http://localhost:4444/wd/hub'))
this way you won't have to worry about anything from selenium, and focus on your project 这样,您就不必担心硒中的任何东西,而将精力集中在您的项目上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.