Mac下selenium3.0+python2.7环境搭建

零、一些注意事项

请注意标题“selenium3.0+python2.7”,

1、selenium目前对Python3支持并不好;

2、selenium2.X与selenium3.X有些配置不一样,selenium 3.x开始,webdriver/firefox/webdriver.py的__init__中,executable_path="geckodriver";而2.x是executable_path="wires",可能会报错:

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 
Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x7f753ad53390>> ignored

一、配置安装

0、python

系统自带了python,所以不需要下载,selenium目前对Python3支持并不好,所以还是使用自带2.7.10。

#python -V     //查看当前python版本查

1、pip

python的安装包管理工具:https://pypi.python.org/pypi/pip

将下载好的pip文件解压,打开终端,cd到该目录,

#sudo python setup.py install
#sudo easy_install pip

这里有个坑,请注意,联网之后默认下载的是selenium3.X,就会出现注意事项里面(2)的问题:

sudo pip install –U selenium
然后要继续执行,下载安装geckodriver,直接去selenium官网下载,或者使用命令:

brew install geckodriver

2、浏览器

下载一个浏览器,例如Firefox或者chrome(推荐),默认安装即可。

3、Python开发环境

Pycharm下载和安装这个你懂得,这里就不多说了。

注意:

Interpreter一定要选对,默认给出的两个可选项很有可能是不对的。

我安装之后默认调用的是2.6.9,改为了2.7.10。

设置:File->Default Settings->Default Project->Project Interpreter(或者->Add local来重新进行选择)

4、测试代码

打开Pycharm,新建project,在project中新建一个后缀为.py的文件,输入以下内容:

from selenium import webdriver
import time

dr = webdriver.Firefox()
time.sleep(5)
print 'Brower will be closed'
dr.quit()
print 'Brower is close'

点击运行后,下方窗口成功打印如下两行,则说明环境配置成功。



猜你喜欢

转载自blog.csdn.net/czc1009/article/details/54693542
今日推荐