Sesame HTTP: Selenium+Chrome on entOS without GUI

Keywords: yum CentOS Google Docker

Are the daily collections of your little buddies torn to death by various clicks in JavaScript? It's hard to find a Selenium+Chrome to solve the problem!

But there's another fact that servers don't have GUI s.··

OK Let's face the music! Never be overwhelmed by such a small difficulty.·······

But the fact before us is that he can't fit all kinds of things. Cheat your papa!

Then I'll save you from fire and water!

The servers are as follows:

[root@spider01 ~]# hostnamectl 
   Static hostname: spider01
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 1c4029c4e7fd42498e25bb75101f85b6
           Boot ID: f5a67454b94b454fae3d75ef1ccab69f
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-514.6.2.el7.x86_64
      Architecture: x86-64

Install Chromeium:


## Install yum source
[root@spider01 ~]# sudo yum install -y epel-release
## Install Chrome
[root@spider01 ~]# yum install -y chromium

Go to this place: https://sites.google.com/a/chromium.org/chrome driver/downloads download the Chrome Driver driver in the / usr/bin/directory:

The results are as follows:

[root@spider01 ~]# ll /usr/bin/ | grep chrom
-rwxrwxrwx. 1 root root   7500280 11 Month 2917:32 chromedriver
lrwxrwxrwx. 1 root root        47 11 Month 3009:35 chromium-browser -> /usr/lib64/chromium-browser/chromium-browser.sh

Install XVFB:

[root@spider01 ~]# yum install Xvfb -y
[root@spider01 ~]# yum install xorg-x11-fonts* -y

Create a new file named xvfb-chromium in / usr/bin / and write the following:


[root@spider01 ~]# cat /usr/bin/xvfb-chromium 
#!/bin/bash
 
_kill_procs() {
  kill -TERM $chromium
  wait $chromium
  kill -TERM $xvfb
}
 
# Setup a trap to catch SIGTERM and relay it to child processes
trap _kill_procs SIGTERM
 
XVFB_WHD=${XVFB_WHD:-1280x720x16}
 
# Start Xvfb
Xvfb :99 -ac -screen 0 $XVFB_WHD -nolisten tcp &
xvfb=$!
 
export DISPLAY=:99
 
chromium --no-sandbox --disable-gpu$@ &
chromium=$!
 
wait $chromium
wait $xvfb

Change the soft connection:


## Changing the Soft Connection Started by Chrome
[root@spider01 ~]# ln -s /usr/lib64/chromium-browser/chromium-browser.sh /usr/bin/chromium
 
 
[root@spider01 ~]# rm -rf /usr/bin/chromium-browser
 
[root@spider01 ~]# ln -s /usr/bin/xvfb-chromium /usr/bin/chromium-browser
 
[root@spider01 ~]# ln -s /usr/bin/xvfb-chromium /usr/bin/google-chrome
 
[root@spider01 ~]# ll /usr/bin/ | grep chrom*
-rwxrwxrwx. 1 root root   7500280 11 Month 2917:32 chromedriver
lrwxrwxrwx. 1 root root        47 11 Month 3009:47 chromium -> /usr/lib64/chromium-browser/chromium-browser.sh
lrwxrwxrwx. 1 root root        22 11 Month 3009:48 chromium-browser -> /usr/bin/xvfb-chromium
-rwxr-xr-x. 1 root root     73848 12 Month 72016 chronyc
lrwxrwxrwx. 1 root root        22 11 Month 3009:48 google-chrome -> /usr/bin/xvfb-chromium
-rwxrwxrwx. 1 root root       387 11 Month 2918:16 xvfb-chromium

Let's see if we can use it.


>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get("http://www.baidu.com")
>>> driver.find_element_by_xpath("./*//input[@id='kw']").send_keys("Ouch sleeping trough ".
>>> driver.find_element_by_xpath("./*//input[@id='su']").click()
>>> driver.page_source

All right, deployment is over! Of course, Docker is such a firecracker for lazy people! Come and see the Docker version here!

docker pull thsheep/chromium-xvfb-py3:master

Python 3.6.3 and Chrome integration

You need to use Dockerfile to repackage and install the Python package you need.

Posted by cal_biker on Wed, 13 Feb 2019 14:15:18 -0800