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

本文介绍如何在Mac上将默认Python版本更改为3.5,并配置Selenium环境,包括安装Selenium及配置WebDriver。同时解决Firefox驱动问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 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

 

转载于:https://www.cnblogs.com/lesleysbw/p/6370379.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值