安装chrome
1、添加chrome的repo源
vi /etc/yum.repos.d/google.repo
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
2、安装google chrome浏览器
yum -y install google-chrome-stable
PS: Google官方源可能在中国无法使用,导致安装失败或者在国内无法更新,可以添加以下参数来安装:
yum -y install google-chrome-stable --nogpgcheck
安装chromedrive
下载chromedrive,官网:http://chromedriver.storage.googleapis.com,需要下载对应版本
wget http://chromedriver.storage.googleapis.com/70.0.3538.97/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv -f chromedriver /usr/local/share/chromedriver
ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
使用Chrome Headless方式
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(driver_path='<path-to-driver>/chromedriver', chrome_options=chrome_options,
service_args=['--verbose', '--log-path=<path-to-log>/chromedriver.log'])
...
Xvfb 和 PyVirtualDisplay
无GUI时,Xvfb为虚拟GUI
yum update
yum install Xvfb
yum -install libXfont
yum install xorg-x11-fonts*
pip install PyVirtualDisplay
检查DISPLAY设置,默认应该是 :1
env | grep DISPLAY
写个小demo测试下效果
vim test.py
# -*- coding:utf-8 -*-
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800,600))
display.start()
driver = webdriver.Chrome("./chromedriver")
driver.get("http://www.baidu.com")
print driver.page_source
driver.quit()
display.stop()
保存文件,执行命令,即可看到效果
python test.py