chromedriver 驱动下载地址:
https://npm.taobao.org/mirrors/chromedriver/
linux centos安装google chrome浏览器使用headless无头模式
用java开发爬虫,或者需要java操作控制浏览器来渲染页面,抓取页面元素,都需要在linux安装google chrome浏览器,然后就可以通过chrome driver来连接操作chrome浏览器了,下面就介绍linux centos安装google chrome浏览器.
1.linux centos7安装 google chrome
下载google浏览器并安装
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
sudo yum install google-chrome-stable_current_*.rpm
一般这样是下载不到的,需要墙,你就只能墙后下载好了再上传再安装。
2.测试运行google chrome
# google-chrome --version
[14479:14479:0222/121214.627009:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
报错了
是因为,不能使用root用户运行沙盒模式,
解决方法是新建用户再测试,linux新建用户参考:linux 添加用户 并添加文件权限
然后再测试
[test@localhost root]$ google-chrome --version
Google Chrome 80.0.3987.116
3.linux 运行google chrome浏览器headless无头模式
用headless模式打印网页pdf,注意要有运行输出文件夹的权限
google-chrome --headless --disable-gpu --print-to-pdf http://www.baidu.com
google-chrome --headless --disable-gpu --no-sandbox --print-to-pdf http://www.baidu.com
系统中文乱码
https://www.cnblogs.com/vekair/p/15878518.html
安装中文系统支持,执行下面三个命令就可以解决:
yum -y groupinstall "X Window System"
yum -y groupinstall chinese-support
yum -y groupinstall Fonts
我只成功执行了第一个和第三个 问题也解决了
安装jdk
上传jdk-8u144-linux-x64.tar.gz
将本地下载的jdk上传到 /home/work/java 目录下
[root@iZ4zeaehxxqhrn553tblkkZ /]# cd /home/work/java
[root@iZ4zeaehxxqhrn553tblkkZ /]# rz
解压
[root@iZ4zeaehxxqhrn553tblkkZ /]# cd /home/work/java
[root@iZ4zeaehxxqhrn553tblkkZ /]# tar -zxvf jdk-8u144-linux-x64.tar.gz
配置环境变量
在/etc/profile文件的末尾加上以下配置:
[root@iZ4zeaehxxqhrn553tblkkZ /]# vim /etc/profile
末尾增加如下内容:文章来源:https://www.toymoban.com/news/detail-486823.html
JAVA_HOME=/home/work/java/jdk1.8.0_144
JRE_HOME=/home/work/java/jdk1.8.0_144/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH
profile文件立即生效文章来源地址https://www.toymoban.com/news/detail-486823.html
[root@iZ4zeaehxxqhrn553tblkkZ /]# source /etc/profile
错误信息记录:
Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.4.0-31-generic x86_64)
解决办法:
chromeOptions.addArguments("--no-sandbox","--disable-dev-shm-usage","window-size=1920x3000","--disable-gpu","--hide-scrollbars","blink-settings=imagesEnabled=false","--headless");
示例代码:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/timbl/goole")
public class GooleController
{
/**
* 获取参数配置列表
*/
@GetMapping("/list")
public Object list()
{
//命令启动浏览器
String os = System.getProperty("os.name");
String drivePath = "D:\\Desktop\\工具demo\\goole\\chromedriver.exe";
if (os.toLowerCase().startsWith("windows")) {
} else if (os.toLowerCase().startsWith("linux")) {
drivePath = "/usr/bin/chromedriver";
}
System.out.println(drivePath);
System.setProperty("webdriver.chrome.driver", drivePath);
//创建无Chrome无头参数
ChromeOptions chromeOptions=new ChromeOptions();
chromeOptions.setHeadless(Boolean.TRUE);
//
chromeOptions.addArguments("--no-sandbox","--disable-dev-shm-usage","window-size=1920x3000","--disable-gpu","--hide-scrollbars","blink-settings=imagesEnabled=false","--headless");
System.out.println("--------------------------------");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("http://www.baidu.com");
String title = driver.getTitle();
System.out.println(title+"--------------------------------");
driver.close();
return "success";
}
}
打包docker镜像
本地提交打包docker
docker commit -m="build in centos7.6.180 and jdk1.8.0_211 and google_chrome" -a="TimBL_" 999 centos7.6_jdk_chrome:v1
打标签
模板:
docker tag [ImageId] 127.0.0.1/devrpa/[ImagesName]:[ImageTag]
例子:
docker tag c8664307af90 127.0.0.1/devrpa/centos7.6_jdk_chrome:v1
推送到远程CCR
模板:
docker push 127.0.0.1/devrpa/[ImagesName]:[ImageTag]
例子:
docker push 127.0.0.1/devrpa/centos7.6_jdk_chrome:v1
到了这里,关于linux centos安装google chrome浏览器使用headless无头模式 制作docker镜像的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!