liunx Centos7无GUI服务器Selenium部署

echo 2019-05-04 AM 3833℃ 0条

安装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

标签: python, Selenium

非特殊说明,本博所有文章均为博主原创。

评论啦~


桂ICP备2023004011号