CentOS7 starts Google Chrome java+Selenium+chrome+chromedriver

Preface: I want to use this technology to automatically capture music. It is currently running successfully on the window. It needs to be run on the Linux Centos service. There are many problems in the configuration, which are hereby recorded.

Reference document: Installing Selenium+chrome+chromedriver+java on CentOS7_Yuanfang丿's Blog-CSDN Blog 

1. Environment

CentOS 7.6 

java (jdk1.8)

Selesium 4.11.0

google-chrome 115
chrome-driver 115

2. Overall logic

What we know clearly is that chrome is installed on the window and comes with chromeDriver. The reason why chrome can be started automatically is because we use ChomeDriver and set some parameters to start.

1. Install google-chrome
2. Install chromeDriver
3. Install XVFB mainly to virtualize an interface so that chrome can be started under CentOS

3. Install chromeDriver

Go to the official website to view the version and download ChromeDriver - WebDriver for Chrome - Downloads

The main thing is that google-chrome and chromeDriver need to match their versions, otherwise an error will be reported.

//下载安装包
wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/115.0.5790.170/linux64/chromedriver-linux64.zip

//解压:
unzip chromedriver_linux64.zip

//然后将解压的chromedriver移动到 /usr/bin目录下:
mv chromedriver /usr/bin/

//给与执行权限:
chmod +x /usr/bin/chromedriver

//检查chromedriver版本:
chromedriver -version


//如果有安装错了,可以清除chromedriver
sudo rm -f /usr/bin/chromedriver

4. Install google-chrome

1. Install chrome. Currently, the connection address for wget download can only be the latest version of Google. I have not found how to specify the version.

//下载chrome(后缀名rpm就是Centos下的安装包后缀,ded是乌班图的安装包后缀)
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm 

//安装 chrome必须要的依赖库
yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts

//安装chrome
rpm -ivh google-chrome-stable_current_x86_64.rpm

//启动chrome
google-chrome

2. Delete google-chrome (because sometimes the wrong version may be installed and you need to delete it)

#杀掉谷歌进程
ps -ef | grep chrome | grep -v grep | awk '{print "kill -9 "$2}'|sh

# 卸载chrome
yum remove google-chrome-stable.x86_64 -y 

3. Check whether chrome is installed successfully

chrome -version

 

4. Run chrome

#Run chrome command

google-chrome

But it reported an error again

//Error message

Missing X server or $DISPLAY
The platform failed to initialize.  Exiting. 
NaCl helper process running without a sandbox!
Most likely you need to configure your SUID sandbox correctly
 

Missing X server or $DISPLAY

Platform initialization failed. Exiting.

NaCl worker process runs without sandbox!

Most likely you need to configure the SUID sandbox correctly

//Modify the startup command
google-chrome --no-sandbox 

It is also inconvenient to add --no-sandbox manually like this. Go to /opt/chrome/google-chrome to modify the configuration.

#exec -a "$0" "$HERE/chrome" "$@"
exec -a "$0" "$HERE/chrome" "$@" --no-sandbox

The above error is that Centos 7.6 has no interface and cannot start chrome like a window. So at this time we need to install XVFB to virtualize an interface so that it can open chrome. The following is to install XVFB .

5. XVFB

 XVFB is an X server that can run on machines without display hardware and physical input devices. That is, you can virtualize an interface on Centos to run the google-chrome browser.

//全局安装Xvfb  
yum install Xvfb -y

//安装Xvfb相关的依赖
yum install xorg-x11-fonts* -y

Create a new file named xvfb-chrom in /usr/bin/ and write the following content :

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

 chmod +x /usr/bin/xvfb-chrome

View current mapping relationship

ll /usr/bin/ | grep chrome 

Change the soft connection started by Chrome 

/*  下面的操作主要就是让xvfb-chrome成为运行的主体,这样chrome在xvfb下就可以运行 */

// 创建一个软连接 
ln -s /etc/alternatives/google-chrome /usr/bin/chrome


//删除google-chrome
rm -rf /usr/bin/google-chrome


//创建一个软连接
ln -s /usr/bin/xvfb-chrome /usr/bin/google-chrome

View the modified mapping relationship

ll /usr/bin/ | grep chrom

The following is a case: pay attention to the order of code execution

public void test(){
            //1. 准备Chrome的配置参数
            ChromeOptions options = new ChromeOptions();
            options.addArguments("headless");  //无界面参数
            options.addArguments("no-sandbox"); //禁用沙盒

            //2. 创建chromeDriver驱动,设置参数
            WebDriver driver = new ChromeDriver(options);



            //3. 在浏览器上执行操作 ,导航到一个网址
            driver.get("https://www.baidu.com/");

            //4. 请求浏览器的信息
            String title = driver.getTitle();
            System.out.println("浏览器的信息==="+title);

            //5. 关闭浏览器
            driver.quit();
}

Guess you like

Origin blog.csdn.net/tengyuxin/article/details/132141242