1. Note: I have tried Centos 6.5 for Selenium, but unfortunately, Centos6.5 is very cumbersome to install and configure, while Centos7 is very smooth, probably because Centos6.5 has poor support for Chrome and Chromedriver. To be exact, chromium is not supported and must be used. Chromium installation is not smooth, so it is recommended that those playing centos6.5 upgrade or restart a centos7 server.
Note: I have tried Centos 6.5 for Selenium, but unfortunately, Centos6.5 is very cumbersome to install and configure, while Centos7 is very smooth, probably because Centos6.5 has poor support for Chrome and Chromedriver. To be exact, chromium is not supported and must be used. Chromium installation is not smooth, so it is recommended that those playing centos6.5 upgrade or restart a centos7 server.Copy the code


Install Selenium+Chrome on unbounded CentOS7 and test using facebook’s php-webdriver

System environment

CentOS Linux 7 (Core)

1
2
3
Copy the code
Operating System: CentOS Linux 7 (Core)
Kernel: Linux 3.10.0-693.17.1.el7.x86_64
Architecture: x86-64
Copy the code

Install the chrome

To install the latest Google Chrome, run the following command as user root:

1
Copy the code
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
Copy the code

To install selenium

Find the latest version on seleniump’s website, and download the selenium-server-standalone. X.x.jar file

Note: Latest version 3.11, not 3.9 (March 2018)

The selenium server – standalone – 3.11.0. Jar

Selenium service initialization

Place the above Selenium in a folder and initialize it by typing the following command

Java jar selenium – server – standalone – 3.11.0. Jar

Note that Java 8 is required. You can install the Java runtime environment JDK by referring to CentOS7

Install chromerriver

Download the latest ChromeDriver package from chromerriver and decompress it to obtain chromedriver.exe

chromedriver_linux64.zip 2018-03-20 15:22:39

Unzip the downloaded file and place it in the following location

/usr/bin/chromedriver

Grant execution authority

chmod +x /usr/bin/chromedriver

Install XVFB

Enter the following command

1
2
Copy the code
# yum install Xvfb -y  
# yum install xorg-x11-fonts* -y
Copy the code

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

vi /usr/bin/xvfb-chrome

12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24Copy the code
#! /bin/bash _kill_procs() { kill -TERM $chrome wait $chrome 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 chrome --no-sandbox --disable-gpu$@ & chrome=$! wait $chrome wait $xvfbCopy the code

Adding Execution Permission

chmod +x /usr/bin/xvfb-chrome

View the current mapping

ll /usr/bin/ | grep chrom

1
2
3
Copy the code
-rwxr-xr-x   1 root root   7874704 Mar 20 14:55 chromedriver
lrwxrwxrwx   1 root root        31 Mar 20 00:24 google-chrome -> /etc/alternatives/google-chrome
lrwxrwxrwx   1 root root        32 Mar 20 14:30 google-chrome-stable -> /opt/google/chrome/google-chrome
Copy the code

Change Chrome launch soft link

1
2
3
Copy the code
ln -s /etc/alternatives/google-chrome /usr/bin/chrome  
rm -rf /usr/bin/google-chrome 
ln -s /usr/bin/xvfb-chrome /usr/bin/google-chrome
Copy the code

View the modified mapping

ll /usr/bin/ | grep chrom

One, two, three, four, fiveCopy the code
-rwxr-xr-x   1 root root   7874704 Mar 20 14:55 chromedriver
lrwxrwxrwx   1 root root        31 Mar 20 00:24 chrome -> /etc/alternatives/google-chrome
lrwxrwxrwx   1 root root        22 Mar 20 00:11 google-chrome -> /usr/bin/xvfb-chromium
lrwxrwxrwx   1 root root        32 Mar 20 14:30 google-chrome-stable -> /opt/google/chrome/google-chrome
-rwxr-xr-x   1 root root       432 Mar 20 00:09 xvfb-chrome
Copy the code

Use Facebook’s PHP-WebDriver test

12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33Copy the code
<? php // An example of using php-webdriver. // Do not forget to run composer install before and also have Selenium server started and listening on port 4444. namespace Facebook\WebDriver; use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\Remote\RemoteWebDriver; require_once('vendor/autoload.php'); // start Chrome with 5 second timeout $host = 'http://localhost:4444/wd/hub'; // this is the default $capabilities = DesiredCapabilities::chrome(); $driver = RemoteWebDriver::create($host, $capabilities, 5000); // navigate to 'http://www.baidu.com/' $driver->get('https://www.baidu.com/'); / / wait until the page is the loaded / / $driver - > wait () - > until (/ / WebDriverExpectedCondition: : titleContains (' baidu ') / /); // print the title of the current page echo "The title is '" . $driver->getTitle() . "'\n"; // print the URI of the current page echo "The current URI is '" . $driver->getCurrentURL() . "'\n"; // print the pagesource of the current page $html_selenium = $driver->getPageSource(); echo $html_selenium; // close the browser $driver->quit();Copy the code