iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >CentOS7 启动谷歌浏览器 java+Selenium+chrome+chromedriver
  • 833
分享到

CentOS7 启动谷歌浏览器 java+Selenium+chrome+chromedriver

seleniumchrome测试工具 2023-09-26 12:09:16 833人浏览 薄情痞子
摘要

前言:自己想使用该技术实现自动化抓取音乐,目前在window上运行成功,需要在linux Centos服务上跑,配置上出现了许多问题,特此记录。 参考文档:CentOS7 安装Selenium+chrome+chromedriver+

前言:自己想使用该技术实现自动化抓取音乐,目前在window上运行成功,需要在linux Centos服务上跑,配置上出现了许多问题,特此记录。

参考文档:CentOS7 安装Selenium+chrome+chromedriver+java_远方丿的博客-CSDN博客 

一、环境

CentOS 7.6 java (jdk1.8)Selesium 4.11.0Google-chrome 115chrome-driver 115

二、 整体逻辑

我们明确的是,在window上是安装了chrome和自带了chromeDriver的,之所以能自动化启动chrome是因为我们使用ChomeDriver,设置了一些参数来启动的。

1. 安装google-chrome
2. 安装chromeDriver
3. 安装XVFB主要是用来虚拟一个界面,以此让chrome在CentOS下启动

三、 安装chromeDriver

去官网查看版本下载 ChromeDriver - WebDriver for Chrome - Downloads

主要是google-chrome 和 chromeDriver要进行版本对应,不然会报错。

//下载安装包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//如果有安装错了,可以清除chromedriversudo rm -f /usr/bin/chromedriver

四、安装google-chrome

安装chrome,目前wget下载的连接地址,只能是当前谷歌最新版本,我也没找到如何指定版本。

//下载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//安装chromerpm -ivh google-chrome-stable_current_x86_64.rpm//启动chromegoogle-chrome

2. 删除google-chrome(因为有时可能安装版本错误,要进行删除操作)

#杀掉谷歌进程ps -ef | grep chrome | grep -v grep | awk '{print "kill -9 "$2}'|sh# 卸载chromeyum remove google-chrome-stable.x86_64 -y 

查看chrome是否安装成功

chrome -version

 

运行chrome

#运行chrome命令

google-chrome

但又报错

//报错信息

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
 

缺少X服务器或$DISPLAY

平台初始化失败。正在退出。

NaCl辅助进程在没有沙箱的情况下运行!

很可能您需要正确配置SUID沙箱

//修改启动命令
google-chrome --no-sandbox 

总是这样手动添加 --no-sandbox也不方便,进入 /opt/chrome/google-chrome修改配置

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

上面的错误就是Centos 7.6下本身无界面,无法像window上启动chrome,所以此时我们要安装XVFB来虚拟一个界面,让其能打开chrome。下面就是安装XVFB

五、 XVFB

 XVFB是一个X服务器,可以在没有显示硬件和物理输入设备的机器上运行。也就是能在Centos上虚拟一个界面让google-chrome浏览器运行。

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

在/usr/bin/  新建一个名叫 xvfb-chrom 的文件写入以下内容

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

更改Chrome启动的软连接 

// 创建一个软连接 ln -s /etc/alternatives/google-chrome /usr/bin/chrome//删除google-chromerm -rf /usr/bin/google-chrome//创建一个软连接ln -s /usr/bin/xvfb-chrome /usr/bin/google-chrome

查看修改后的映射关系

ll /usr/bin/ | grep chrom

下面是案例:注意代码执行顺序

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();}

来源地址:https://blog.csdn.net/tengyuxin/article/details/132141242

--结束END--

本文标题: CentOS7 启动谷歌浏览器 java+Selenium+chrome+chromedriver

本文链接: https://www.lsjlt.com/news/418465.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作