当前位置:网站首页>Centos7 install python3 + selenium + chrome + chromedriver

Centos7 install python3 + selenium + chrome + chromedriver

2020-11-10 14:21:39 . Weiyang

Centos7 install python3+Selenium+chrome+chromedriver detailed
python2 and python3 coexistence ,Selenium Wrong handling
to update Centos Source

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
perhaps
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# After downloading , Run the following command :
yum clean all
yum makecache

 

1.Python3 Installation and python2 coexistence

wget http://mirrors.sohu.com/python/3.6.2/Python-3.6.2.tar.xz
yum install libffi-devel expat-devel gdbm-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
mv /usr/bin/python /usr/bin/python.bak
tar -xvJf  Python-3.6.2.tar.xz
cd Python-3.6.2
./configure prefix=/usr/local/python3
make && make install
make clean
ln -s /usr/local/python3/bin/python3 /usr/bin/python

 

python -V Check to see if python3
python2 -V Check to see if python2
If it's normal to see , Please continue to set .yum need python2 edition , So we have to modify yum Configuration of ./usr/libexec/urlgrabber-ext-down It also needs to be modified python2

vi /usr/bin/yum
# Put the first line of the document python Change to python2

#!/usr/bin/python2                                                                          
import sys                                                                                  
try:                                                                                        
    import yum                                                                              
except ImportError:

...... Continue to modify urlgrabber-ext-down
vi /usr/libexec/urlgrabber-ext-down
# Change the first line as above python Change to python2

#! /usr/bin/python2                                                                         
#  A very simple external downloader                                                        
#  Copyright 2011-2012 Zdenek Pavlas

 

python2 and python3 coexistence : Default pip yes python2,python3 How to configure ? If pip It's not installed either , Just install pip

yum -y install epel-release
yum install python-pip

ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

 

To configure pip Source according to their own needs , Or not

mkdir ~/.pip
vi pip.conf

[global]
timeout = 60
index-url = https://pypi.doubanio.com/simple

 

2.chrome Installation and chromedriver download
chrome Download and install

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

   

chromedriver download , I downloaded the latest version .chrome It's also the latest version

https://npm.taobao.org/mirrors/chromedriver/2.40/chromedriver_linux64.zip

chromedriver Download and copy , To /usr/local/bin/ Under the table of contents , And give executable rights : chmod -R 744 /usr/local/bin/chromedriver

Otherwise, when you run or restart a program , It's easy to report mistakes :selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH

   

3. install selenium, Use yes python3

pip3 install selenium

   

Beginning of the test :
Create a name for test Catalog , The directory structure is as follows :

[root@localhost test]# tree                                                         
├── chromedriver
└── test.py

 

test.py The test code is as follows :chrome Interface browsing

# -*- coding:utf-8 -*-

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(executable_path="/root/test/chromedriver", chrome_options=options)
driver.get("https://www.baidu.com")
print(driver.page_source)
driver.quit()

 

Test run to see

python test.py

   

After running, report error decisively

 raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist

 

modify test.py add –no-sandbox Perfect solution . Of course use selenium There may be other mistakes , I'll collect some error fixes in other articles .


from selenium import webdriver

options = webdriver.ChromeOptions()

# Server does not need --    windows Next need --
options.add_argument(' headless')
options.add_argument(' no-sandbox')

driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver',chrome_options=options)
driver.get("https://www.baidu.com")

print(driver.page_source)
print(driver.title)
driver.close()

 

reference https://blog.csdn.net/lizhou828/article/details/102954993

版权声明
本文为[. Weiyang]所创,转载请带上原文链接,感谢