linux---centos7 安装chrome和chromedriver,并处理不能用root打开的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/diyiday/article/details/83824878

1.安装浏览器

指定yum 源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

安装

curl https://intoli.com/install-google-chrome.sh | bash

在这里插入图片描述

安装后,执行:

google-chrome-stable --no-sandbox --headless --disable-gpu --screenshot https://www.baidu.com/

报这个错误?我猜想是没有UI界面的原因,到这里可以判断安装成功。

  1. 安装chromedriver

下载:

https://npm.taobao.org/mirrors/chromedriver/

下载的chromedriver需要和当前的chrome版本相对应,

google-chrome-stable 可以查看当前版本

unzip 解压

在这里插入图片描述

安装完成

建立软连接:

ln -s /opt/google/chromedriver /usr/bin/chromedriver

3.测试:

#coding=utf8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
#driver = webdriver.Chrome()
driver.get("https://www.baidu.com")

print(driver.page_source)
#driver.quit()

通过这个安装的版本,存在一个问题,就是通过这样的方式安装,是最新的浏览器。
如果安装旧一点的版本,需要卸载当前版本,然后安装旧版本,安装了旧版本,对应的chromedriver也需要替换

#查看已安装的软件
rpm -qa|grep google-chrome 
#卸载高版本google-chrome
rpm -e google-chrome +软件详细名称
# 安装已经下载的google-chrome***.rpm
rpm ivh install  google-chrome***.rpm

不能用root打开

启动报错

[31560:31560:0207/085601.085852:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

运行如下命令

google-chrome --no-sandbox

仍会报错:但是可以打开浏览器

root@node00:~# [0207/085735.495265:ERROR:nacl_helper_linux.cc(310)] NaCl helper process running without a sandbox!Most likely you need to configure your SUID sandbox correctly

重启

然后编辑一下配置信息

# vim /usr/bin/google-chrome

修改如下,即可正常打开浏览器了

将 exec -a "$0" "$HERE/chrome" "$@"  改为
exec -a "$0" "$HERE/chrome" "$@" --user-data-dir --no-sandbox

猜你喜欢

转载自blog.csdn.net/diyiday/article/details/83824878