centos 安装selenium+chrome+chromedriver+Xvfb

1.安装selenium

pyhon version: 2.7

pip install selenium
pip install pyvirtualdisplay

 

2.安装chrome和配置chromedriver

# 下载chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm --no-check-certificate
# 安装chrome
yum install ./google-chrome-stable_current_x86_64.rpm

#google-chrome --version 查看chrome版本

# 下载对应的chromedriver:https://npm.taobao.org/mirrors/chromedriver
wget https://npm.taobao.org/mirrors/chromedriver/85.0.4183.38/chromedriver_linux64.zip
unzip chromedriver_linx64.zip
mv chromedriver /usr/bin/

#chromedriver  --version 查看chromedriver 版本

通过chrome的版本选择对应的 chromedriver 版本

chromedriver地址:https://npm.taobao.org/mirrors/chromedriver

 

3.安装Xvfb (无界面访问浏览器)

安装说明

安装此软件的原因是在centos上,chromeDriver必须使用无头模式,当有不使用无头模式的需求时就需要安装此软件,否则chromeDriver无法正确启动chrome

安装Xvfb

yum install Xvfb -y
yum install xorg-x11-fonts* -y

在/usr/bin/目录下创建xvfb-chrome文件

vim /usr/bin/xvfb-chrome

在xvfb-chrome文件中输入以下内容

#!/bin/bash

_kill_procs() {
kill -TERM $chrome
wait $chrome
kill -TERM $xvfb
}

# Setup a trap to catch SIGTERM and relay it to child processes
trap _kill_procs SIGTERM

XVFB_WHD=${XVFB_WHD:-1280x720x16}

# Start Xvfb
Xvfb :99 -ac -screen 0 $XVFB_WHD -nolisten tcp &
xvfb=$!

export DISPLAY=:99

chrome --no-sandbox --disable-gpu$@ &
chrome=$!

wait $chrome
wait $xvfb

 添加执行权限:

chmod +x /usr/bin/xvfb-chrome

查看当前的映射关系:

ll /usr/bin/ | grep chrom


映射关系如下所示:

lrwxrwxrwx 1 root root 31 Nov 18 14:25 chrome -> /etc/alternatives/google-chrome
lrwxrwxrwx 1 root root 20 Nov 18 14:25 google-chrome -> /usr/bin/xvfb-chrome
lrwxrwxrwx 1 root root 32 Jun 28 18:39 google-chrome-stable -> /opt/google/chrome/google-chrome
-rwxr-xr-x 1 root root 422 Nov 18 14:24 xvfb-chrome

更改chrome启动的软连接:

ln -s /etc/alternatives/google-chrome /usr/bin/chrome
rm -rf /usr/bin/google-chrome
ln -s /usr/bin/xvfb-chrome /usr/bin/google-chrome

此时再次查看映射关系,如下所示:

lrwxrwxrwx 1 root root 31 Nov 18 14:36 chrome -> /etc/alternatives/google-chrome
lrwxrwxrwx 1 root root 20 Nov 18 14:36 google-chrome -> /usr/bin/xvfb-chrome
lrwxrwxrwx 1 root root 32 Jun 28 18:21 google-chrome-stable -> /opt/google/chrome/google-chrome
-rwxr-xr-x 1 root root 422 Nov 18 14:36 xvfb-chrome

以上步骤操作完成后即可在centos环境下使用非无头浏览器,此时chromeDriver创建时不能再添加以下参数:

options.addArguments("--headless")

4.测试

from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800,600))
display.start()
options = webdriver.ChromeOptions()
options .add_argument('--headless')
options .add_argument('--no-sandbox')
options .add_argument('--disable-dev-shm-usage')
browser = webdriver.Chrome(chrome_options=options)
browser.get("http://www.baidu.com")
print(browser.page_source)
browser.quit()
driver.quit()

 

你可能感兴趣的:(linux,服务器,centos)