Ubuntu 22.094 LTS中undetected-chromedriver无法连接Chrome的解决求助
解决Ubuntu上undetected-chromedriver无法连接Chrome的问题
以下是针对你遇到的unknown error: cannot connect to chrome错误的具体解决步骤:
1. 验证Chrome基础运行能力
先确认Chrome本身能在headless模式下正常启动:
在终端执行:
google-chrome --headless --no-sandbox --disable-dev-shm-usage https://google.com
如果命令执行失败,说明Chrome依赖缺失,Ubuntu 22.04下需要安装必要依赖:
sudo apt install libgbm-dev libasound2 libxss1 libappindicator3-1 libnss3
安装完成后再重新测试Chrome启动。
2. 调整undetected-chromedriver初始化代码
- 替换参数名:selenium 4及后续版本中,
chrome_options参数已更名为options,同时补充关键启动参数:
from selenium.webdriver.chrome.options import Options import undetected_chromedriver as uc chrome_options = Options() chrome_options.add_argument("--headless") chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") # 解决/dev/shm空间不足问题 chrome_options.add_argument("--remote-debugging-port=9222") # 指定调试端口 # 优先让undetected-chromedriver自动匹配驱动,移除executable_path参数 driver = uc.Chrome(options=chrome_options)
- 若必须指定驱动路径,先确保chromedriver有执行权限:
chmod +x /home/samuel04/chromedriver
3. 升级undetected-chromedriver版本
你当前使用的3.2.1版本较旧,可能存在与Chrome 108的兼容性问题,执行适配版本升级:
pip install --upgrade undetected-chromedriver==3.5.3 # 该版本适配Chrome 108
4. 检查用户权限
确保运行脚本的用户对Chrome安装目录(通常为/usr/bin/google-chrome)和chromedriver文件有访问权限,避免因权限不足导致启动失败。
内容的提问来源于stack exchange,提问作者prodohsamuel
相关产品推荐
相关产品推荐

