Centos7安 裝python3+Selenium+chrome+chromedriver

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可能會出現其他的錯誤,我會在其他文章收集些錯誤解決辦法。

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

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(executable_path="/root/test/chromedriver", chrome_options=options)
driver.get("https://www.baidu.com")
print(driver.page_source)
driver.quit()
————————————————
版權聲明:本文爲CSDN博主「maggie_up」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Maggie_up/article/details/80790344

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章