安装Chrome
指定yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
下载并安装
curl https://intoli.com/install-google-chrome.sh | bash
ldd /opt/google/chrome/chrome | grep "not found"
测试浏览器
google-chrome-stable --no-sandbox --headless --disable-gpu --screenshot https://www.baidu.com/
安装ChromeDriver
下载地址:http://chromedriver.storage.googleapis.com/index.html
查看版本号(注意下载对应版本号的驱动)
google-chrome --version
下载安装包
wget https://npm.taobao.org/mirrors/chromedriver/72.0.3626.7/chromedriver_linux64.zip
解压安装包
unzip chromedriver_linux64.zip
将可执行文件移到bin目录 (Mac环境改为 /usr/local/bin 即可)
mv chromedriver /usr/bin/
测试代码
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(options=options)
driver.get("https://www.baidu.com")
print(driver.page_source)
driver.quit()