install chrome+chromeDriver+Xvfb on centos7

install chrome

create yum source

# cd /etc/yum.repos.d/
# vim google-chrome.repo

Create yum source information

[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

install google-chrome

# yum -y install google-chrome-stable --nogpgcheck

Chrome installation path After using the above steps to install, the chrome installation path is in the /opt/google/chrome directory

Install chrome Driver

check chrome version

# google-chrome -version

Download the corresponding version of chromeDriver

Query the chromeDriver version corresponding to the chrome version from https://npm.taobao.org/mirrors/chromedriver/, and the larger version is sufficient. For example, the latest version of chrome is 78.0.3904.97, and you only need to find the chromeDriver version corresponding to 78 That's it.

After finding the corresponding version, use the following command to download the chromeDriver compressed package on Linux:

# mkdir /usr/chromedriver
# cd /usr/chromedriver
# wget https://npm.taobao.org/mirrors/chromedriver/version/chromedriver_linux64.zip

version is the complete version number of chromeDriver

Unzip the compressed package after downloading:

# unzip chromedriver_linux64.zip
Give permissions to the chromeDriver folder:

# chmod +x /usr/chromedriver/chromedriver

Install Xvfb

Installation Notes

The reason for installing this software is that on centos, chromeDriver must use headless mode. When there is a need to not use headless mode, you need to install this software, otherwise chromeDriver cannot start chrome correctly.

Install Xvfb

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

Create the xvfb-chrome file in the /usr/bin/ directory
# vim /usr/bin/xvfb-chrome
Enter the following content in the xvfb-chrome file

#!/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

Add execute permissions:

# chmod +x /usr/bin/xvfb-chrome
View the current mapping relationship:

# ll /usr/bin/ | grep chrom
The mapping relationship is as follows:

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

Change the soft connection started by 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

At this point, look at the mapping relationship again, as follows:

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

After the above steps are completed, you can use a non-headless browser in the centos environment. At this time, the following parameters cannot be added when creating chromeDriver:

options.addArguments("--headless")

Guess you like

Origin blog.csdn.net/weixin_42553583/article/details/120608266