一:安装chrome (以下是默认下载最新版)

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

查看版本:google-chrome --version

二:安装chromedriver

  1. 根据上一步安装的chrome,通过查看chrome版本,去查找相对应的chromedriver版本。

参考地址:http://chromedriver.chromium.org/downloads

这里有详细的chrome和chromedriver 版本对应关系。

比如此刻找到的对应版本的下载连接为https://chromedriver.storage.googleapis.com/2.45/chromedriver_linux64.zip

在centos上下载该zip文件。

wget  https://chromedriver.storage.googleapis.com/2.45/chromedriver_linux64.zip

三:添加Chromedriver 软链接

  • 比如Chromedriver的位置为 /root/chromedriver

ln -s /root/chromedriver /usr/bin/chromedriver

安装完成后,可保存以下py文件进行测试


# -*- coding:utf-8 -*-
 
from selenium import webdriver
 
options = webdriver.ChromeOptions()
options.add_argument('--headless')  # 确保无头
options.add_argument('--disable-gpu')  # 无需要gpu加速
options.add_argument('--no-sandbox')  # 无沙箱
driver = webdriver.Chrome(executable_path="/root/chromedriver", chrome_options=options)  # 添加软链接后是不需要写路径的
 
driver.get("https://www.baidu.com")
print(driver.page_source)
driver.quit()