[英]Failed - Download Error when downloading in Selenium headless Chrome
我正在尝试使用Selenium在Chrome中下载文件。 我发现无头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)
这是我的代码:
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)
在搜索“ Download error
常见原因之后,我排除了以下内容:
删除解决方法后,所有文件都可以正常下载,但是我将无法以无头模式下载。 任何的意见都将会有帮助。
附加信息:
ChromeDriver版本: 2.40.565498
Chrome版本: 67.0.3396.87
您可以尝试禁用安全性 ,也许它会起作用
我建议不要在无头模式下运行,我们也在Linux服务器上运行selenium,并且我们选择使用单独的selenium docker容器(Web服务器)并创建远程驱动程序,而不是本地驱动程序:
检查此页面以获取更多信息
安装docker之后,您可以使用以下命令启动selenium
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm --network host selenium/standalone-chrome:3.141.59-neon
确保具有--network host
以便可以从localhost
访问4444
端口。 之后,您只需执行以下操作即可创建一个远程驱动程序
from selenium import webdriver
driver = webdriver.Remote(command_executor = exec_path or 'http://localhost:4444/wd/hub'))
这样,您就不必担心硒中的任何东西,而将精力集中在您的项目上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.