Sesame HTTP: Using Selenium + Chrome on CentOS without GUI

<div class="iteeye-blog-content-contain" style="font-size: 14px;">
<p>Are you tossed with various click events of JavaScript in your daily collection? Finally found a Selenium+Chrome can solve the problem! </p>
<p>But another ▄█▀█● fact is in front of you, the server has no GUI.</p>
<p>Okay! We have to overcome difficulties! He must not be overwhelmed by this little difficulty...</p>
<p>However, the fact in front of him is that he can't pretend to be anything! Pit father! </p>
<p>Then I'm here to save you from fire and water! </p>
<p>The server is as follows:</p>
<pre name="code" class="java">[root@spider01 ~]# hostnamectl
   Static hostname: spider01
         Icon name: computer-vm
           Chassis: vm
        Machine ID : 1c4029c4e7fd42498e25bb75101f85b6
           Boot ID: f5a67454b94b454fae3d75ef1ccab69f
    Virtualization:

       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-514.6.2.el7.x86_64
      Architecture: x86-64
</pre>
<p>Install Chromeium:</p>
<pre name ="code" class="java">​## Install yum source
[root@spider01 ~]# sudo yum install -y epel-release
## Install Chrome
[root@spider01 ~]# yum install -y chromium</pre >
<p> Go to this place: https://sites.google.com/a/chromium.org/chromedriver/downloads Download the ChromeDriver driver and put it in the /usr/bin/ directory:</p>
<p>The result is as follows :</p>
<pre name="code" class="java">[root@spider01 ~]# ll /usr/bin/ | grep chrom
-rwxrwxrwx. 1 root root 7500280 Nov 29 17:32 chromedriver
lrwxrwxrwx. 1 root root 47 Nov 30 09:35 chromium-browser -> /usr/lib64/chromium-browser/chromium-browser.sh</pre>
<p> Install XVFB:</p>
<pre name= "code" class="java">​[root@spider01 ~]# yum install Xvfb -y
[root@spider01 ~]# yum install xorg-x11-fonts* -y
</pre>
<p> New in /usr /bin/ A file called xvfb-chromium writes the following:</p>
<pre name="code" class="java">

​ [ 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

​</pre>
<p> 更改软连接:</p>
<pre name="code" class="java">​

## 更改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月 29 17:32 chromedriver
lrwxrwxrwx. 1 root root        47 11月 30 09:47 chromium -> /usr/lib64/chromium-browser/chromium-browser.sh
lrwxrwxrwx. 1 root root        22 11月 30 09:48 chromium-browser -> /usr/bin/xvfb-chromium
-rwxr-xr-x. 1 root root     73848 12月  7 2016 chronyc
lrwxrwxrwx. 1 root root        22 11月 30 09:48 google-chrome -> /usr/bin/xvfb-chromium
-rwxrwxrwx. 1 root root       387 11月 29 18:16 xvfb-chromium

​</pre>
<p> 来瞅瞅能不能用哦:</p>
<pre name="code" class="java">​>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get("http://www.baidu.com")
>>> driver.find_element_by_xpath("./*//input[@id='kw']").send_keys("ouch")
>>> driver.find_element_by_xpath("./* //input[@id='su']").click()
>>> driver.page_source
</pre>
<p> Well deployed! Of course, Docker is such a hot thief for lazy people! Come and see the Docker version here! </p>
<pre name="code" class="java">​docker pull thsheep/chromium-xvfb-py3:master
</pre>
<p> Python3.6.3 and Chrome integration ready</p>
< p>You need to use the Dockerfile yourself to repackage and install the Python packages you need. </p>
</div>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326399592&siteId=291194637