Centos 7.6 install seleniu

  1. Check Linux version
[ root@penguin selenium]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810(Core)
  1. Install selemium

2.1 Install selenium via pip, first install pip:

[ root@penguin selenium]#yum -y install epel-release
[ root@penguin selenium]#yum -y install python-pip

2.2 If you are prompted to update pip, execute the following command:

[ root@penguin selenium]#pip install --upgrade pip

2.3 pip install selenium

[ root@penguin src]# pip install selenium

2.4 Uninstall Mozilla firefox that comes with Centos

 mv /etc/firefox//etc/firefox.bak
 mv /usr/lib64/firefox//usr/lib64/firefox.bak
 mv /usr/bin/firefox /usr/bin/firefox.old

2.5 Download and unzip firefox

[ root@penguin src]# ls -l /usr/local/src/firefox-66.0.3.tar.bz2 
- rw-r--r--1 root root 62089056 Apr 3016:47/usr/local/src/firefox-66.0.3.tar.bz2
[ root@penguin src]#tar xjvf firefox-66.0.3.tar.bz2 

2.6 Create a virtual display

yum install Xvfb libXfont xorg-x11-fonts*
pip3 install  pyvirtualdisplay

2.7 Create a new symbolic link, test firefox start with xshell

[ root@penguin src]#cd firefox/
[ root@penguin src]#ln -s /usr/lib64/firefox/firefox /usr/bin/firefox

[ root@penguin selenium]# firefox -version
Mozilla Firefox 66.0.3

[ root@penguin src]#firefox No error is reported here, firefox starts normally, if there is an error, please/etc/hosts must have the following line:
127.0.0.1 localhost

2.8 Install google chrome, solve root can not run chrome

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

[ root@penguin selenium]# google-chrome -version
Google Chrome 74.0.3729.108

Solve root cannot run chrome

vi /usr/bin/google-chrome

Add at the end of the file:
if[[-n "$CHROME_USER_DATA_DIR"]]; then
 # Note: exec -a below is a bashism.
 exec -a "$0""$HERE/chrome"  \
 - - user-data-dir="$CHROME_USER_DATA_DIR""$@"else
 exec -a "$0""$HERE/chrome"--user-data-dir="/root/.config/google-chrome/"  \
 " $@"--no-sandbox
fi

2.9 Download and configure firefox corresponding webdriver--geckodriver

cd /usr/local/src/
tar xzvf geckodriver-v0.24.0-linux64.tar.gz
cp /usr/local/bin/geckodriver /usr/bin/geckodriver

2.10 Download and configure the webdriver corresponding to google chrome--chromedriver_linux64

Download chromedriver_linux64.zip

Unzip, the configuration is as follows:

cp /usr/local/src/chromedriver_linux64/chromedriver /usr/bin/

root@penguin selenium]# chromedriver -version
ChromeDriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29})

  1. test

3.1 Write code to test firefox

#! /usr/bin/env python3

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://www.163.com')print(driver.title)
# driver.quit()

3.2 Write code to test google chrome

#! /usr/bin/env python3

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.baidu.com')print(driver.title)

driver.quit()

  1. Exception problem solving

firefox:

File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 174,in __init__
 keep_alive=True)
 File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157,in __init__
 self.start_session(capabilities, browser_profile)
 File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252,in start_session
 response = self.execute(Command.NEW_SESSION, parameters)
 File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321,in execute
 self.error_handler.check_response(response)
 File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242,in check_response
 raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching setof capabilities

Webdriver and firefox version do not match, you can find the corresponding webdriver version to solve

Traceback(most recent call last):
 File "./test.py", line 8,in<module>
 driver = webdriver.Firefox()  
 File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py", line 164,in __init__
 self.service.start()
 File "/root/.pyenv/versions/3.6.4/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 104,in start
 raise WebDriverException("Can not connect to the Service %s"% self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver

Uninstall the firefox that comes with the system, reinstall and set it as root to start, and refer to the setting of step 2.7/etc/hosts

chrome:

The main problem is caused by root being unable to start google-chrome, just configure the no-sandbox option, refer to step 2.8 for details

Recommended Posts

Centos 7.6 install seleniu
1.5 Install Centos7
Centos6 install Python2.7.13
CentOS7.2 install Mysql5.7.13
CentOS install Redmine
Centos7 install Python 3.6.
CentOS 7 install Docker
CentOS7 install GlusterFS
CentOS 7.4 install Zabbix 3.4
CentOS7 install Docker
Centos6.5 install Tomcat
CentOS install Python 3.6
Vmware install CentOS6
centos7 install docker-ce 18.01.0
CentOS 7.2 install MariaDB
Centos7 install Python2.7
CentOS 7.3 install Zabbix3
Centos7 install LAMP+PHPmyadmin
CentOS install mysql
CentOS install openjdk 1.8
CENTOS6.5 install CDH5.12.1 (1)
CentOS install PHP
CentOS6 install mist.io
Centos7 install Docker
CentOS7 install mysql
centOs install rabbitMQ
CentOS 7 install MySQL 5.6
Centos7 install Nginx
CentOS6.5 install CDH5.13
Centos7 install docker18
Centos install Python3
centos7 install docker
CentOS install jdk
centos7 install nginx-rtmp
CentOS8 install MySQL8.0
Centos6.3 install KVM
CentOS install PostgreSQL 9.1
CentOS7 install mysql8
CentOS 7 install Java 1.8
CentOS8 install fastdfs6.06
CentOS 7 install Gitlab
Centos 7 install PostgreSQL
CentOS7 install MySQL8
CentOS 7 install Java 1.8
CentOS 6 install Docker
centos 6.5 install zabbix 4.4
Centos8 install Docker
CentOS6.8 install python2.7
CentOS install nodejs 8
CentOS6.5 install GNS3
centos 7.5 install mysql5.7.17
Centos7 install MySQL8.0-manual
CentOS7 install Kubernetes 1.16.3
VirtualBox install centos7
centos7 install lamp
Install centos7 and connect
Install Docker on Centos7
install LNMP on centos7.4
CentOS 8 install ZABBIX4.4 guide
Install Java on Centos 7
CentOS6.5 offline install MySQL5.6.26