selenium python教程(mac下怎么搭建selenium python环境)

本文目录
- mac下怎么搭建selenium python环境
- 如何搭建Python3.4+Selenium
- Selenium 和python是啥关系是否相关参考文档
- 怎样开始写第一个基于python的selenium脚本
mac下怎么搭建selenium python环境
零、一些注意事项
请注意标题“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“,可能会报错:
[html] view plain copy
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
#python -V //查看当前python版本查
#sudo python setup.py install
#sudo easy_install pip
sudo pip install –U selenium
- 然后要继续执行,下载安装geckodriver,直接去selenium官网下载,或者使用命令:
brew install geckodriver
from selenium import webdriver
import time
dr = webdriver.Firefox()
time.sleep(5)
print ’Brower will be closed’
dr.quit()
print ’Brower is close’
一、配置安装
0、python
系统自带了python,所以不需要下载,selenium目前对Python3支持并不好,所以还是使用自带2.7.10。
[html] view plain copy
1、pip
python的安装包管理工具:
将下载好的pip文件解压,打开终端,cd到该目录,
[html] view plain copy
这里有个坑,请注意,联网之后默认下载的是selenium3.X,就会出现注意事项里面(2)的问题:
[html] view plain copy
[html] view plain copy
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的文件,输入以下内容:
[python] view plain copy
点击运行后,下方窗口成功打印如下两行,则说明环境配置成功。
如何搭建Python3.4+Selenium
详细步骤:
1.下载32位的python3.4 ,安装,注意在安装过程中,选择pip
2.添加系统环境变量
C:\Python34;C:\Python34\Scripts;C:\Program Files\Google\Chrome\Application(后面会用到)
3.使用pip命令,安装selenium
cd Python34
然后输入: pip install -U selenium
安装成功后,显示:
Runing setup.py install foe selenium
Successfully installed selenium-2.48.0
4.检测selenium是否安装成功。
在Python的IDLE中,输入:
from selenium import webdriver
如果没有报错,则selenium安装成功。
Selenium 和python是啥关系是否相关参考文档
Selenium是一个软件测试工具; python是一种面向对象、直译式计算机程序设计语言.
Selenium分成Selenium IDE, Selenium RC (Remote Control), 和Selenium Grid. 他们都是基于Selenium Core的. 这里给你说说Selenium IDE和RC吧. Selenium IDE是Firefox的一个插件, 它可以录制一系列的过程(比如google中输入一个单词, 点搜索按钮,...,). Selenium IDE可以把这个过程录制号, 并且转化成多种语言, 比如:Java, C#, Perl, PHP, Python,等等吧.
那么这时候我们就可以这些语言对应的编译器(比如Java我们用ECLIPSE; C#用Visual Studio)把从Selenium IDE经过录制, 得到并转化好的程序代码在这些编译器里运行, 运行的时候, 程序会调用Selenium RC来实现对刚才这段过程录制的远程操作 (可以控制打开一个firefox或者IE的浏览器进行回放, 也就是测试的过程.
简单地说就是 Selenium可以用python语言来调用它(Selenium RC)进行测试.
不知道我说明白了没有.
怎样开始写第一个基于python的selenium脚本
1、下载并安装python
2、安装selenium下载并解压缩selenium-2.32.0.tar.gz. 把selenium-2.32.0\py\下的selenium整个文件夹放入Python33\Lib\site-packages目录下。
3 下载Eclipse后安装pydev插件
4 打开eclipse创建pydev project,并添加一个Pydev Module 输入一下代码
# coding=gbk
from selenium import webdriver
from selenium.webdriver.common import by
if __name__ == ’__main__’:
firefoxDriver=webdriver.Firefox()
firefoxDriver.get(“
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Ie()
driver.get(“xxx“)
elem = driver.find_element_by_name(“wd“)
elem.send_keys(“selenium“)
elem.send_keys(Keys.RETURN)
完成上述步骤,你已经写了第一个基于python的selenium 脚本了

更多文章:
majority of(the majority of 和 a majority of的区别以及用法例句)
2026年10月11日 07:40
another time(another time和other time的区别)
2026年10月11日 05:00







