当前位置:网站首页>Centos7安 装python3+Selenium+chrome+chromedriver

Centos7安 装python3+Selenium+chrome+chromedriver

2020-11-10 14:21:39 .未央

Centos7安装python3+Selenium+chrome+chromedriver详细
python2和python3共存,Selenium错误的处理
更新Centos源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#下载完后,运行下面命令:
yum clean all
yum makecache

 

1.Python3安装与python2共存

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 检查下是不是python3
python2 -V 检查下是不是python2
如果上面正常显示,请继续设置下。yum需要python2版本,所以我们还要修改yum的配置。/usr/libexec/urlgrabber-ext-down也需要修改python2

vi /usr/bin/yum
#把文件第一行python改成python2

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

......继续修改urlgrabber-ext-down
vi /usr/libexec/urlgrabber-ext-down
#跟上面一样修改第一行python改成python2

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

 

python2和python3共存:默认pip是python2,python3需要如何配置?如果pip也没有安装,就先安装pip

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

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

 

配置pip源按自己需要,也可以不配置

mkdir ~/.pip
vi pip.conf

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

 

2.chrome安装和chromedriver下载
chrome下载安装

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

   

chromedriver下载,我下载的是最新版本。chrome也是最新版本

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

chromedriver下载后拷贝,到/usr/local/bin/目录下,并且赋可执行的权限: chmod -R 744 /usr/local/bin/chromedriver

不然在运行程序或或者重启程序时,容易报错:selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH

   

3.安装selenium,使用是的python3

pip3 install selenium

   

测试开始:
创建一个名字为test目录,目录结构如下:

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

 

test.py测试代码如下:chrome界面浏览

# -*- 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()

 

测试运行看看

python test.py

   

运行完果断报错

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

 

修改test.py加上–no-sandbox完美解决。当然使用selenium可能会出现其他的错误,我会在其他文章收集些错误解决办法。


from selenium import webdriver

options = webdriver.ChromeOptions()

# 服务器不需要--    windows 下 需要--
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()

 

借鉴https://blog.csdn.net/lizhou828/article/details/102954993

版权声明
本文为[.未央]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/wy919/p/13940130.html