简体   繁体   English

失败-在Selenium无头Chrome中下载时出现下载错误

[英]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常见原因之后,我排除了以下内容:

  1. Incorrect download path: some files with the same download path can be downloaded, but others can 下载路径错误:可以下载某些具有相同下载路径的文件,但其他可以
  2. File path too long: some files that can be downloaded have longer paths than those with errors 文件路径太长:某些可下载的文件的路径比错误的路径长

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.

相关问题 使用 chrome headless 和 selenium 下载 - Downloading with chrome headless and selenium 在 Selenium 中使用无头 Chrome 时出错 - Error when using headless Chrome in Selenium Selenium Chrome Headless 下载文件 - Selenium Chrome Headless download file 使用Selenium在Python中进行Chrome无头文件下载 - Chrome headless file download with Selenium in Python 阻止在 Selenium 无头 Chrome [python] 上下载图像 - Block the download of images on Selenium headless Chrome [python] 使用带有 python selenium 的无头 chrome 时出现“403 Forbidden”错误 - Getting '403 Forbidden' error when using headless chrome with python selenium 设置 selenium 与 headless chrome 错误:WebDriverException:消息:未知错误:Chrome 无法启动:异常退出 - Setup selenium with headless chrome error: WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally Selenium 在 Chrome 中有效,但在使用无头 Chrome 时无效 - Selenium works in Chrome but not when using headless Chrome 在python中使用Selenium在无头chrome中下载文件时文件未保存 - File Not Saving While Downloading File in Headless chrome using Selenium in python 未知错误:Chrome 无法启动:通过 Selenium 和 Python 使用带有无头 chrome 的 user-data-dir 参数退出异常错误 - unknown error: Chrome failed to start: exited abnormally error using user-data-dir argument with headless chrome through Selenium and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM