You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用远程Selenium上传超1GB文件时出现<Errno 32 Broken pipe>错误求助

遇到这种大文件上传的Broken pipe问题太头疼了,我之前也碰到过类似的情况,本质是Selenium默认的文件上传机制在处理大文件时的局限性——它会把本地文件编码成base64通过HTTP请求传给远程服务器,1GB的文件转成base64后会变成1.3GB左右,很容易触发连接超时或中断。给你几个可行的解决方案,按优先级排序:

解决方案1:绕过Selenium的文件传输,直接使用远程服务器上的文件路径

如果你的远程Selenium服务器能访问到目标文件(比如通过共享存储、SFTP预先把文件传到远程机器),这是最稳定的解决办法:直接在send_keys里传入远程服务器上的文件绝对路径,这样Selenium不需要传输整个文件内容,只需要告诉浏览器文件在远程机器上的位置,彻底避免大文件的HTTP传输。

修改后的示例代码:

from selenium.webdriver import ChromeOptions, Remote
options = ChromeOptions()
options.add_argument("--start-maximized")
# 替换成远程服务器上的文件绝对路径
remote = Remote(command_executor="http://localhost:9515", desired_capabilities=options.to_capabilities())
remote.find_element_by_css_selector('input[name="file"]').send_keys('/remote/server/path/to/file.iso')

解决方案2:调整Selenium远程连接的超时设置

Selenium默认的HTTP请求超时时间不足以处理大文件的传输,你可以手动设置RemoteConnection的超时时间,给大文件足够的传输时间:

from selenium.webdriver import ChromeOptions, Remote
from selenium.webdriver.remote.remote_connection import RemoteConnection

options = ChromeOptions()
options.add_argument("--start-maximized")

# 设置更长的超时时间(比如300秒=5分钟,可根据文件大小调整)
conn = RemoteConnection("http://localhost:9515", timeout=300)
remote = Remote(command_executor=conn, desired_capabilities=options.to_capabilities())

remote.find_element_by_css_selector('input[name="file"]').send_keys('path/to/file.iso')

解决方案3:调整ChromeDriver的参数优化上传

尝试添加以下ChromeOptions参数,优化浏览器的文件上传处理逻辑,减少连接中断的概率:

options = ChromeOptions()
options.add_argument("--start-maximized")
# 仅在测试环境使用,关闭跨域安全限制
options.add_argument("--disable-web-security")
# 允许文件访问本地文件
options.add_argument("--allow-file-access-from-files")
# 禁用Chrome的新网络服务,避免大文件传输时的连接问题
options.add_argument("--disable-features=NetworkService")

补充说明

  • 小文件(70MB以下)能成功是因为base64编码后的体积还在默认HTTP请求的处理范围内,不会触发超时或连接断开。
  • 如果无法预先上传文件到远程服务器,优先尝试方案2,再结合方案3的参数调整,大概率能解决问题。

内容的提问来源于stack exchange,提问作者Vladimir Yahello

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:55:03