Linux下安装Chrome

同事有需求在 Jupyter 上通过安装 Chrome 对某个图像进行绘制并下载为 jpg 格式的图片,因此记录一下在 Linux 系统下安装 Chrome 的过程

安装浏览器

CentOS/RHEL/Amazon Linux(rpm)

下载地址: Google Chrome

上述地址需要使用代理访问,否则无法下载

1
yum install -y ./google-chrome-stable_current_x86_64.rpm

Ubuntu(deb)

下载地址: Google Chrome

上述地址需要使用代理访问,否则无法下载

1
apt-get --fix-broken install -y ./google-chrome-stable_current_amd64.deb

安装驱动

通用方法

Chrome 官方手册

Selenium官方手册

下载地址 Google Chrome Driver

上述地址需要使用代理访问,否则无法下载

寻找到指定版本后,下载 Linux 系统次新版的 zip 包,建议版本号小于 100,以免出现问题1

解压缩并上传 chromedriver 文件到服务器,一般建议使用 /usr/local/bin 或其他在 PATH 中定义的目录

使用 Selenium

安装依赖

1
2
3
4
5
pip install pypandoc
pip install pyecharts
pip install snapshot-selenium
pip install selenium
pip install snapshot-phantomjs

Q&A

deb 系无法使用 chrome 驱动

需要安装依赖

1
2
3
apt-get install -y apt-utils
apt-get install -y libnss3-dev
apt-get install -y libxss-dev

unknown error: DevToolsActivePort file doesn't exist

需要添加以下参数

1
2
3
4
5
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox") # 使用 root 权限运行
options.add_argument("--remote-debugging-port=9222") # 解决报错问题核心
options.headless = True # 无头模式,已不支持使用 --headless 参数

具体讨论参见Chrome Driver Issues 该网址来源 Github Issues


  1. 因版本号≥100,部分网页会取前两位截取为 10 版本,导致提示 Chrome 版本过旧,无法访问,详情↩︎