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%]

再次執行測試程序,結果如下,已經解決中文顯示問題:

 

 

安裝內網前,最好對內網的包做個檢查

請點擊查看

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM