centos 安装 chrome 无GUI 情况下
1. 安装Xvfb、google-chrome
2. 测试
3. 本例子使用
chrome-driver 2.35.528139
google-chrome 70.0.3538.77-1.x86_64
一、安装 google-chrome
-
添加 google安装源
vim /etc/yum.repos.d/google.repo[google]name=Google-x86_64baseurl=http://dl.google.com/linux/rpm/stable/x86_64enabled=1gpgcheck=0gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
-
安装google chrome
yum update
yum install Xvfb -y
yum install libXfont -y
yum install xorg-x11-fonts* -y
yum install google-chrome-stable -y
-
测试
pip install selenium
pip install pyvirtualdisplay
代码
from selenium import webdriver
executable_path = './chromedriver'
service_log_path ='./chrome.log'
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-gpu') # 无
chrome_options.add_argument('–disable-plugins')
chrome_options.add_argument('–disable-java') ### 禁用java
chrome_options.add_argument('–disable-popup-blocking') ## 禁止弹出拦截
chrome_options.add_argument('–disable-images') ### 禁止图像加载
chrome_options.add_argument('--verbose')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--no-sandbox') ### 不打开浏览器应用页面
chrome_options.add_argument('–disk-cache-size={}'.format(30*1024*1024))
chrome_options.add_argument('–single-process')
chrome_options.add_argument('–incognito') ###隐身模式或无痕模式,不保存cookies 缓存文件、历史记录、下架记录
driver = Chrome(chrome_options=chrome_options,
executable_path=executable_path,
service_log_path=service_log_path)
driver.get("http://www.baidu.com")
html = driver.page_source
print(html)
driver.quit()
chrome其他参数
chrome_options.add_argument('-user-data-dir={}'.format(user_data_path)) ###user data
chrome_options.add_argument('–disk-cache-dir={}'.format(cache_path)) ###
chrome_options.add_argument('--log-path={}'.format(os.path.join(log,'chrome.log')))
注意:chrome版本与chromedriver版本要一致。不一致式会出错
驱动地址:http://chromedriver.storage.googleapis.com/index.html
版本号对应关系:https://yq.aliyun.com/ziliao/306392