0%

selenium3环境配置及其他细节优化

之前在windows使用的firefox和chrome都是绿色版,配置的selenium3一直没有成功,于是尝试着在Linux下配置,成功配置了!

准备

OS: Linux ubuntu 16.04
python3:Python 3.5.2
Firefox默认安装

下载安装selenium3

确定操作系统架构

打开终端,输入sudo uname -p

  • 若返回i686,证明为32为操作系统
  • 若返回x86_64,证明为64位操作系统

安装selenium3

对于同时安装python2与python3的环境而言,采用

1
python3 -m pip install selenium3

若提示权限不够,加上sudo,即

1
sudo python3 -m pip install selenium3

下载geckodriver

若firefox版本过高,需要安装geckodriver
这里是下载地址
选择对应操作系统版本的下载,然后解压,并进入解压之后的目录,接着
安装geckodriver 记住要在解压之后的父目录操作
cp geckodriver /usr/local/bin/

安装xvfb

sudo apt-get install xvfb

安装pyvirtualdisplay

1
python3 -m pip install pyvirtualdisplay

测试selenium3

复制以下代码,保存为test.py

1
2
3
4
5
6
7
8
9
10
11
12
from selenium import webdriver
import time

driver = webdriver.Firefox()

driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("python开发")
driver.find_element_by_id("su").click()

time.sleep(5) #等待5s,关闭浏览器。

driver.quit()

然后运行

若报错selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable may have wrong permissions

  • 定位到之前geckodriver的目录,设置权限为-rwxr-xr-x
    即sudo chmod 755 geckodriver

其他优化

PYPI 更换国内源

在安装过程中,发现默认的安装源太慢,可以通过更换国内源来提速。
笔者这边较快的源是阿里源和清华源

  • WINDOWS更换
    找到python的安装目录,定位到\Lib\site-packages\pip\目录,然后打开cmdoptions.py,搜索index_url
    得到如下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    index_url = partial(
    Option,
    '-i', '--index-url', '--pypi-url',
    dest='index_url',
    metavar='URL',
    default=PyPI.simple_url,

    help="Base URL of Python Package Index (default %default). "
    "This should point to a repository compliant with PEP 503 "
    "(the simple repository API) or a local directory laid out "
    "in the same format.")

修改为以下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
index_url = partial(
Option,
'-i', '--index-url', '--pypi-url',
dest='index_url',
metavar='URL',
#default=PyPI.simple_url,
#清华源
#default='https://pypi.tuna.tsinghua.edu.cn/simple',
#阿里源
default='https://mirrors.aliyun.com/pypi/simple/',

help="Base URL of Python Package Index (default %default). "
"This should point to a repository compliant with PEP 503 "
"(the simple repository API) or a local directory laid out "
"in the same format.")

清华源和阿里源可以自己选择,只需要将另外的注释掉就可以了

  • Linux更换
    在/.pip/下新建或修改pip.conf为
    1
    2
    [global]
    index-url = https://mirrors.aliyun.com/pypi/simple/

注意
在添加源的时候,要使用加密链接,即https。若使用http,需要添加—trusted-host这一参数
以上方法是采用修改默认源的方法,还可以在安装的时候,指定安装源,使用pip -i参数.如

1
sudo python3 -m pip install selenium3 -i https://mirrors.aliyun.com/pypi/simple/

同理,若使用http,需要添加—trusted-host这一参数

pip 升级所有package

  • 1
    pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

参考来源

  • 采用pip 先列出outdate,再升级
    即
  1. 1
    python3 -m pip list --outdate --format=freeze >>update3.txt

update3.txt为输出outdate的文件

  1. 打开update3.txt,去掉所有package后面的版本信息,也就是==以及以后的信息
    如pyOpenSSL==0.15.1 修改为pyOpenSSL
  2. 1
    python3 -m pip install -r update3.txt -U

ubuntu清理垃圾

  1. 清理apt载安装软件缓存文件
    $sudo apt-get clean

  2. 清理期软件包
    $sudo apt-get autoclean

  3. 自卸载没软件依赖关系软件
    $sudo apt-get autoremove

坚持原创技术分享,您的支持将鼓励我继续创作!