[ root@penguin selenium]# cat /etc/redhat-release
CentOS Linux release 7.6.1810(Core)
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})
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()
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