CentOS7.X安装Chrome和chromedriver

频道:服务器运维 日期:
一、下载安装Chrome

安装相关依赖

yum install liberation-fonts -y
yum install vulkan -y
yum -y install redhat-lsb*
yum -y install libXss*
yum -y install libappindicator*
yum -y install libgbm
下载google-chrome 最新RPM安装包

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
安装google-chrome

rpm -ivh google-chrome-stable_current_x86_64.rpm
查看google-chrome版本

google-chrome --version
二、下载安装chromedriver

下载对应版本的chromedriver(这里以108.0.5359.71版本为例)

wget https://chromedriver.storage.googleapis.com/108.0.5359.71/chromedriver_linux64.zip
unzip解压压缩包

unzip chromedriver_linux64.zip
设置可执行权限

chmod +x chromedriver
将其移至/usr/bin/目录下

mv chromedriver /usr/bin/
命令行输入chromedriver验证是否安装成功

[root@iZ2zeg ~]# chromedriver
Starting ChromeDriver 108.0.5359.71 (1e0e3868ee06e91ad636a874420e3ca3ae3756ac-refs/branch-heads/5359@{#1016}) on port 9515
Only local connections are allowed.
三、selenium验证能否正常使用

from selenium import webdriver
 
option = webdriver.ChromeOptions()
option.add_argument('headless')        #linux系统需设置无可视化界面
option.add_argument('--no-sandbox')    #解决DevToolsActivePort文件不存在的报错
driver = webdriver.Chrome(chrome_options=option)
driver.get('https://www.baidu.com')
print(driver.title)
driver.quit()
输出“百度一下,你就知道”说明能够正常使用。

出现如下报错信息:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
添加option.add_argument('--no-sandbox') ,即可解决找不到DevToolsActivePort的报错