python3使用selenium3的坑

网络看了很多的文章,大部分都是不完整, 还有很多误导性极强的教程 ,特别是chromedriver这东西.简直一堆坑.

一首先是安装python3.6.5

root@ubuntu:~# add-apt-repository ppa:jonathonf/python-3.6

root@ubuntu:~# apt-get update

root@ubuntu:~# apt-get install python3.6

root@ubuntu:~# python3.6

如果出现以下内容证明成功:

Python 3.6.5 (default, May  3 2018, 10:08:28)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

二:设置默认python3对应为python3.6

 root@ubuntu:~# update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1

root@ubuntu:~# update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2

root@ubuntu:~# update-alternatives --config python3

看到以下内容:

There are 2 choices for the alternative python3 (providing /usr/bin/python3).


  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.6   2         auto mode
 1            /usr/bin/python3.5   1         manual mode
 2            /usr/bin/python3.6   2         manual mode
Press <enter> to keep the current choice[*], or type selection number:

回车完毕

三:升级pip3

root@ubuntu:~# apt-get install python3-pip

root@ubuntu:~# pip3 install --upgrade pip

四:安装selenium

root@ubuntu:~# pip3 install selenium

五:安装chrome浏览器

下载谷歌浏览器的deb安装包

##  https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

$sudo apt install libappindicator1 libindicator7

$sudo dpkg -i google-chrome-stable_current_amd64.deb

$sudo apt -f install        ####修复依赖关系

六:安装chromedriver

    chromedriver版本对照表如下

ChromeDriver v2.41 (2018-07-07)----------Supports Chrome v67-69

ChromeDriver v2.40 (2018-06-07)----------Supports Chrome v66-68
ChromeDriver v2.39 (2018-05-30)----------Supports Chrome v66-68
ChromeDriver v2.38 (2018-04-17)----------Supports Chrome v65-67
ChromeDriver v2.37 (2018-03-16)----------Supports Chrome v64-66
ChromeDriver v2.36 (2018-03-02)----------Supports Chrome v63-65
ChromeDriver v2.35 (2018-01-10)----------Supports Chrome v62-64

比如要按照自己系统chrome浏览器版本下载对应chromedriver版本
将下载chromedriver放到/usr/bin/下
=== 代码需要指定chromedriver的路径,不是系统chrome浏览器的路径,这个要千万注意.如下:
chromedriver = "/usr/local/bin/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get(url="https://www.qq.com")
测试以上代码,如果chrome浏览器能正常出来并打开qq那就是chromedriver已安装好.
原本路径可以直接写到系统的环境变量里的,只可惜,如果这样做,crontab去调用chrome的时候就是报错.

=== 如需要将脚本放到crontab里去执行需要原来的执行命令前加上 export DISPLAY=:0&& 如下
export DISPLAY=:0&&/usr/bin/python3 /apps/run_brower_text.py >> /apps/auto.log 2>&1



原文地址:https://www.cnblogs.com/yehui/p/9390219.html