1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > centos7安装chrome+chromeDriver+Xvfb

centos7安装chrome+chromeDriver+Xvfb

时间:2024-05-20 09:38:15

相关推荐

centos7安装chrome+chromeDriver+Xvfb

安装chrome

创建yum源

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

创建yum源信息

[google-chrome]name=google-chromebaseurl=/linux/chrome/rpm/stable/$basearchenabled=1gpgcheck=1gpgkey=https://dl-/linux/linux_signing_key.pub

安装google-chrome

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

chrome安装路径 使用上述步骤安装完成之后,chrome的安装路径在/opt/google/chrome目录下

安装chromeDriver

查看chrome版本

# google-chrome -version

下载对应版本的chromeDriver

从/mirrors/chromedriver/查询与chrome版本对应的chromeDriver版本,大版本对应即可,如当前chrome最新的版本是78.0.3904.97,只需要查找与78对应的chromeDriver版本即可。

找到对应的版本后,在linux上使用以下命令下载chromeDriver压缩包:

# mkdir /usr/chromedriver# cd /usr/chromedriver# wget /mirrors/chromedriver/version/chromedriver_linux64.zip

version为chromeDriver的完整版本号

下载之后解压压缩包:

# unzip chromedriver_linux64.zip

给chromeDriver文件夹赋予权限:

# chmod +x /usr/chromedriver/chromedriver

安装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 $chromewait $chromekill -TERM $xvfb}# Setup a trap to catch SIGTERM and relay it to child processestrap _kill_procs SIGTERMXVFB_WHD=${XVFB_WHD:-1280x720x16}# Start XvfbXvfb :99 -ac -screen 0 $XVFB_WHD -nolisten tcp &xvfb=$!export DISPLAY=:99chrome --no-sandbox --disable-gpu$@ &chrome=$!wait $chromewait $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-chromelrwxrwxrwx 1 root root 20 Nov 18 14:25 google-chrome -> /usr/bin/xvfb-chromelrwxrwxrwx 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-chromelrwxrwxrwx 1 root root 20 Nov 18 14:36 google-chrome -> /usr/bin/xvfb-chromelrwxrwxrwx 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")

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。