Mac OS下搭建python+selenium环境

1.安装python mac自带python2.7 可以不安装
安装pip sudo easy_install pip

2.安装 PyCharm
进入 PyCharm 的官网下载安装https://www.jetbrains.com/pycharm/
破解:
a.修改 /etc 目录下的 hosts 文件

sudo vim /etc/hosts

b.在hosts文件中添加以下信息

0.0.0.0 account.jetbrains.com  

c.启动 pycharm 选择 Activate code 进行激活

d.复制以下信息

EB101IWSWD-eyJsaWNlbnNlSWQiOiJFQjEwMUlXU1dEIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiIiwiYXNzaWduZWVFbWFpbCI6IiIsImxpY2Vuc2VSZXN0cmljdGlvbiI6IkZvciBlZHVjYXRpb25hbCB1c2Ugb25seSIsImNoZWNrQ29uY3VycmVudFVzZSI6ZmFsc2UsInByb2R1Y3RzIjpbeyJjb2RlIjoiSUkiLCJwYWlkVXBUbyI6IjIwMTgtMTAtMTQifSx7ImNvZGUiOiJSUzAiLCJwYWlkVXBUbyI6IjIwMTgtMTAtMTQifSx7ImNvZGUiOiJXUyIsInBhaWRVcFRvIjoiMjAxOC0xMC0xNCJ9LHsiY29kZSI6IlJEIiwicGFpZFVwVG8iOiIyMDE4LTEwLTE0In0seyJjb2RlIjoiUkMiLCJwYWlkVXBUbyI6IjIwMTgtMTAtMTQifSx7ImNvZGUiOiJEQyIsInBhaWRVcFRvIjoiMjAxOC0xMC0xNCJ9LHsiY29kZSI6IkRCIiwicGFpZFVwVG8iOiIyMDE4LTEwLTE0In0seyJjb2RlIjoiUk0iLCJwYWlkVXBUbyI6IjIwMTgtMTAtMTQifSx7ImNvZGUiOiJETSIsInBhaWRVcFRvIjoiMjAxOC0xMC0xNCJ9LHsiY29kZSI6IkFDIiwicGFpZFVwVG8iOiIyMDE4LTEwLTE0In0seyJjb2RlIjoiRFBOIiwicGFpZFVwVG8iOiIyMDE4LTEwLTE0In0seyJjb2RlIjoiUFMiLCJwYWlkVXBUbyI6IjIwMTgtMTAtMTQifSx7ImN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度Chrome版本对应的chromeDriver版本下载
ChromeDriver v2.41 (2018-07-27)———-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
http://chromedriver.storage.googleapis.com/index.html
chromedriver解压后放到Python或者其他配置了环境变量的目录下。

5.在pycharm新建project,选择python2.7 新建python file

这里写图片描述
输入

from selenium import webdriver
import time
dr = webdriver.Chrome()
dr.get('http://baidu.com')
time.sleep(5)
print 'Browser will close'
dr.quit()
print 'Browser closed'

运行
这里写图片描述

如果selenium报错
在pycharm里打开file -> prefernce -> Project Interpreter选择python2.7 点击下方的+导入selenium即可
这里写图片描述

猜你喜欢

转载自blog.csdn.net/nikita1995/article/details/82499549