Mac python3.5 + Selenium 开发环境配置

一. python 3.5

1. 下载

2. Mac默认为2.7,所以这里主要介绍如何将系统Python默认修改为3.5。

原理:

1)Mac自带的python环境在:

python2.7: /System/Library/Frameworks/Python.framework/Versions/2.7

其中解释器在该目录下的:./bin/python2.7

2)用户安装的python环境在:

python3.5: /Library/Frameworks/Python.framework/Versions/3.5

 

在Mac启动时,会加载系统配置文件(包括~/.bash_profile),所有默认的命令的路径将会被配置文件(例如:bash_profile)中的路径所覆盖,并且后面的路径覆盖前面的路径。所以我们需要修改bash_profile文件

1. touch .bash_profile

新建bash_profile文件

2. open .bash_profile

打开bash_profile文件,并且在最后添加一句:

alias python="/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5"

3. source .bash_profile

执行使生效

最后我们输入python可以看见默认版本为 3.5

 

二. Selenium环境配置

因为我们现在已经更改了当前python为3.5版本,所以我们直接输入:

python -m pip install selenium

不需要

sudo pip install --user -U selenium

因为这样会让selenium安装在python2.7下面:


三. 配置webdriver

下载Chrome+Selenium IDE

检查:

在Pycharm中运行:

from selenium import webdriver
import time

driver = webdriver.Firefox(executable_path="/Users/lesley/Downloads/geckodriver")
driver.get("http://www.baidu.com/")
driver.find_element_by_id("kw").send_keys("testSeleniumForMac")
driver.find_element_by_id("su").click()
time.sleep(5)
driver.quit()

注意:如果不添加executable_path,则会报错:

selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

这是因为高版本的Firefox需要下载第三方支持插件geckodriver,下载地址:

http://docs.seleniumhq.org/download/#side_plugins

 

posted @ 2017-02-06 15:03  lesleysbw  阅读(1177)  评论(0编辑  收藏  举报