[Selenium] Using Selenium+Chrome in the Centos6.5 environment

foreword

Recently, I was trying to do a project to automatically log in to the web page. I used Selenium+browsermob-proxy+chrome. At the beginning, I didn't know that chrome could also perform headless interface operations, so I tried Chrome's interface operation and PhantomJS interfaceless operation. Two methods. But I found that PhantomJS is not so comfortable to use, there are problems that cause the program to be good and bad, until I recently learned about the interfaceless operation of chrome and gave up PhantomJS.
After the chrome browser was updated to version 59, the headless mode was added to the official version, that is, chrome can be manipulated into a no-interface mode.
The point I want to talk about today is that I developed it locally (window environment), and then finally deployed it to a Linux environment. Everything works fine when I develop locally, but after deploying to the server (the server uses Centos6.5), I encounter various problems.

problems encountered

1. The premise of using chromeDriver is to have a chrome browser

Since I'm using chrome locally, I'm not going to switch browsers either.
Different from PhantomJS, PhantomJS itself is an interfaceless browser, so there is no need for a browser in the development environment, while chrome is an interfaced browser. What we are using now is its headless mode, so that it does not display the interface, But in fact it still operates in the browser, so there must be a chrome browser in the development environment, otherwise an error will be reported when you execute chromedriver:

org.openqa.selenium.WebDriverException: unknown error: cannot find Chrome binary

This is the prompt that we need to have a chrome browser.
But it should be noted that chrome no longer supports Linux 32-bit, nor does it support Centos, so if you are using Centos, you can only install Chromium (similar to chrome, both are Google products, this is an open source project). Install Chromium on Centos 6.5:

# cd /etc/yum.repos.d
# wget http://people.centos.org/hughesjr/chromium/6/chromium-el6.repo
# yum install chromium

2. Running chromeDriver requires the support of a higher version of the runtime library

After the installation is successful, you can first test whether it is feasible, execute the chromeDriver program, and then report an error:

NSS_VersionCheck("3.26") failed. NSS >= 3.26 is required

That is to say, to upgrade the NSS version, upgrade the version:

# yum install nss.x86_64

View existing NSS versions:

rpm -qa | grep nss

Then execute and continue to report the error:

/lib64/libc.so.6: version `GLIBC_2.14' not found (required by /usr/ydx/chromedriver)

See the previous article for the solution.
emm.... I have been solving bugs.... I feel that the feeling of encountering a bug is really complicated. When I can't solve it, it is very explosive. When I solve it successfully, I have a sense of accomplishment... The process of solving the bug is also the process of growth~~

3. Install the virtual graphics environment Xvfb

But even if I solve the above problems, I still can't run it successfully, and I continue to report errors. Wow, what a blast! ! !
write picture description here

This is because Linux does not have a graphical interface, and the use of selenium+chrome requires a graphical interface, so we need to install a virtual graphical environment: Xvfb is a display server that implements the X11 display service protocol. Unlike other display servers, Xvfb performs all graphics operations in memory and does not require any display device.
Install Xvfb:

# cd /tmp
# wget http://vault.centos.org/6.5/os/x86_64/Packages/xorg-x11-server-Xvfb-1.13.0-23.el6.centos.x86_64.rpm
# yum install xorg-x11-server-Xvfb-1.13.0-23.el6.centos.x86_64.rp   
m

After installation we run Xvfb:

# Xvfb -ac :7 -screen 0 1280x1024x8 -extension RANDR -nolisten inet6 &

"-extension RANDR -nolisten inet6" is used in the execution statement because Xvfb uses ipv6 by default. If it is not added, an error will be reported:

XSERVTransSocketOpenCOTSServer: Unable to open socket for inet6
_XSERVTransOpen: transport open failed for inet6/VM_0_3_centos:7
_XSERVTransMakeAllCOTSServerListeners: failed to open listener for inet6

The installation is successful when the following is displayed:
write picture description here

Start chrome:

export  DISPLAY=:7 (和上一步的number号相同)

Reference articles:
1. Chrome failed to start: exited abnormally
2. Tutorial on running Java Selenium on the server
3. Selenium uses chrome's headless mode
4. Unable to open socket for inet6

Guess you like

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