centos7谷歌chrome内网部署演示

 

上传需要的包,注释网关创建内网环境

[root@localhost ~]# ls
anaconda-ks.cfg  chrome  mcw4  mcw4.tar.gz  mcwchromerpm.tar.gz
[root@localhost ~]# rz 

[root@localhost ~]# ls
anaconda-ks.cfg  chrome  google-chrome-stable_current_x86_64.rpm  mcw4  mcw4.tar.gz  mcwchromerpm.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg  chrome  google-chrome-stable_current_x86_64.rpm  mcw4  mcw4.tar.gz  mcwchromerpm.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg  chrome  google-chrome-stable_current_x86_64.rpm  mcw4  mcw4.tar.gz  mcwchromerpm.tar.gz
[root@localhost ~]# mkdir mcw2
[root@localhost ~]# ls
anaconda-ks.cfg  chrome  google-chrome-stable_current_x86_64.rpm  mcw2  mcw4  mcw4.tar.gz  mcwchromerpm.tar.gz
[root@localhost ~]# yum localinstall google-chrome-stable_current_x86_64.rpm --downloadonly --downloaddir=/root/mcw2/
[root@localhost ~]# ls
anaconda-ks.cfg  chrome  google-chrome-stable_current_x86_64.rpm  mcw2  mcw4  mcw4.tar.gz  mcwchromerpm.tar.gz
[root@localhost ~]# tar zcvf mcw2chrome.tar.gz mcw2
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
[root@localhost ~]# grep -i gateway /etc/sysconfig/network-scripts/ifcfg-ens33
#GATEWAY="10.0.0.2"
[root@localhost ~]# ping 223.5.5.5
connect: Network is unreachable
[root@localhost mcw2]# createrepo .
Spawning worker 0 with 82 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@localhost mcw2]# pwd
/root/mcw2

创建yum仓库

[root@localhost mcw2]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo  CentOS-CR.repo  CentOS-Debuginfo.repo  CentOS-fasttrack.repo  CentOS-Media.repo  CentOS-Sources.repo  CentOS-Vault.repo  epel.repo
[root@localhost yum.repos.d]# vim mcw.repo
[root@localhost yum.repos.d]# ping 223.5.5.5
connect: Network is unreachable
[root@localhost yum.repos.d]# cat mcw.repo 
[self]
name=local
baseurl=file:///root/mcw2/
gpgcheck=0
[root@localhost yum.repos.d]# cd
[root@localhost ~]# ls
anaconda-ks.cfg  chrome  google-chrome-stable_current_x86_64.rpm  mcw2  mcw2chrome.tar.gz  mcw4  mcw4.tar.gz  mcwchromerpm.tar.gz
[root@localhost ~]# yum localinstall -y google-chrome-stable_current_x86_64.rpm 
[root@localhost yum.repos.d]# mkdir mcw
[root@localhost yum.repos.d]# mv *.repo  mcw
[root@localhost yum.repos.d]# ls
mcw
[root@localhost yum.repos.d]# cp mcw/mcw.repo .  #不移动会报错
[root@localhost yum.repos.d]# ls
mcw  mcw.repo
[root@localhost yum.repos.d]# cd
[root@localhost ~]# ls
anaconda-ks.cfg  chrome  google-chrome-stable_current_x86_64.rpm  mcw2  mcw2chrome.tar.gz  mcw4  mcw4.tar.gz  mcwchromerpm.tar.gz
[root@localhost ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: self
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
[root@localhost ~]# 
[root@localhost ~]# 

根据指定版本的chrome浏览器rpm包安装

[root@localhost ~]# yum localinstall -y google-chrome-stable_current_x86_64.rpm 
Loaded plugins: fastestmirror
Examining google-chrome-stable_current_x86_64.rpm: google-chrome-stable-88.0.4324.150-1.x86_64
[root@localhost ~]# /opt/google/chrome/chrome
[3206:3206:1019/014410.961876:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
[root@localhost ~]# /opt/google/chrome/google-chrome 
[3208:3208:1019/014421.892979:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
[root@localhost ~]# /opt/google/chrome/google-chrome --disable-setuid-sandbox --user-data-dir --no-sandbox >/dev/null 2>&1 &
[1] 3217
[root@localhost ~]# ps -ef|grep chrome
root       3234   1464  0 01:45 pts/0    00:00:00 grep --color=auto chrome
[1]+  Exit 1                  /opt/google/chrome/google-chrome --disable-setuid-sandbox --user-data-dir --no-sandbox > /dev/null 2>&1

可以忽略的错误修改步骤

#注意:下面这个vim步骤可以忽略掉,是个错误的步骤。后面排查花了很久才发现这步不需要修改为exec -a "$0" "$HERE/chrome" "$@" --user-data-dir --no-sandbox,修改后程序启用不了浏览器。不确定在windows上启用谷歌浏览器图形化界面是否能用到,但是程序调用是不用修改的

[root@localhost ~]# systemctl restart network
[root@localhost ~]# ping 223.5.5.5
PING 223.5.5.5 (223.5.5.5) 56(84) bytes of data.
64 bytes from 223.5.5.5: icmp_seq=1 ttl=128 time=113 ms
^C
--- 223.5.5.5 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 113.390/113.390/113.390/0.000 ms
[root@localhost ~]# yum -y install python
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Nothing to do
[root@localhost ~]# ls /etc/yum.repos.d/mcw
CentOS-Base.repo  CentOS-CR.repo  CentOS-Debuginfo.repo  CentOS-fasttrack.repo  CentOS-Media.repo  CentOS-Sources.repo  CentOS-Vault.repo  epel.repo  mcw.repo
[root@localhost ~]# cp /etc/yum.repos.d/mcw/*.repo /etc/yum.repos.d/
cp: overwrite ‘/etc/yum.repos.d/mcw.repo’? y^H
[root@localhost ~]# yum -y install python3
[root@localhost ~]# pip3 install selenium
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting selenium
  Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
    100% |████████████████████████████████| 911kB 684kB/s 
Collecting urllib3 (from selenium)
  Downloading https://files.pythonhosted.org/packages/af/f4/524415c0744552cce7d8bf3669af78e8a069514405ea4fcbd0cc44733744/urllib3-1.26.7-py2.py3-none-any.whl (138kB)
    100% |████████████████████████████████| 143kB 1.5MB/s 
Installing collected packages: urllib3, selenium
Successfully installed selenium-3.141.0 urllib3-1.26.7

自动化测试脚本验证谷歌浏览器使用

[root@localhost ~]# cat test.py 
from selenium.webdriver.chrome.options import Options
from time import sleep
from selenium import webdriver
# 创建一个参数对象,用来控制chrome以无界面模式打开
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')#实例化一个浏览器对象
bro = webdriver.Chrome(executable_path=r'/root/chrome/chromedriver',chrome_options=chrome_options)
bro.get('https://www.baidu.com')
sleep(2)
bro.save_screenshot('1.png')
print(bro.page_source)
sleep(2)
bro.quit()
[root@localhost ~]# python3 test.py 
Traceback (most recent call last):
  File "test.py", line 16, in <module>
    bro = webdriver.Chrome(executable_path=r'/root/chrome/chromedriver',chrome_options=chrome_options)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist


前面有个错误步骤修正:不需要修改/usr/bin/google-chrome
[root@localhost ~]# tail -2 /usr/bin/google-chrome
exec -a "$0" "$HERE/chrome" "$@"
#exec -a "$0" "$HERE/chrome" "$@" --user-data-dir --no-sandbox
[root@localhost ~]# cat test.py
from selenium.webdriver.chrome.options import Options
from time import sleep
from selenium import webdriver

# 创建一个参数对象,用来控制chrome以无界面模式打开
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')#实例化一个浏览器对象
bro = webdriver.Chrome(executable_path=r'/root/chrome/chromedriver',chrome_options=chrome_options)
bro.get('https://www.baidu.com')
sleep(2)
bro.save_screenshot('1.png')
print(bro.page_source)
sleep(2)
bro.quit()

执行结果图:

 

 

 

 谷歌浏览器中文显示问题解决

将安装包下载到lang目录下,然后直接rpm -ivh *.rpm包安装

#组安装的好像直接rpm -ivh *.rpm就可以安装。yum安装的可能存在依赖关系,不能直接rpm -ivh装上,但是组安装的好像没有这方面的限制。

[root@localhost mcw5]# cd ..
[root@localhost ~]# ls
将Fonts需要的包下载到lang目录下
[root@localhost yum.repos.d]# yum groupinstall -y Fonts #组安装的貌似yum装不起来,直接rpm 安装所有的,貌似没有依赖关系
[root@localhost ~]# cd lang  
[root@localhost lang]# rpm -ivh *.rpm  #
Preparing...                          ################################# [100%]

再次执行测试程序,结果如下,已经解决中文显示问题:

 

 

安装内网前,最好对内网的包做个检查

请点击查看

 

posted @ 2021-10-19 10:56  马昌伟  阅读(208)  评论(0编辑  收藏  举报