`

python + selenium 自动化框架搭建

 
阅读更多

python + selenium 自动化框架搭建

1、下载并安装python(http://www.python.org/getit/,selenium暂时不支持python3,这里使用2.7.3版本)或命令:sudo apt-get install python-virtualenv


2、下载并安装setuptools(http://pypi.python.org/pypi/setuptools,这里使用setuptools-0.6c11.win32-py2.7版本),或命令:apt-get install python-setuptools


3、 下载pip(http://pypi.python.org/pypi/pip,这里使用pip-1.2.1.tar.gz版本),解压缩之后,使用 cmd命令:python setup.py install(如果python命令使用不成功,请配置下python的环境变量),打开cmd命令,进 入python的scripts目录(比如c:\python27\scripts),输入easy_install pip。


4、 安装selenium(http://pypi.python.org/pypi/selenium or http://pypi.python.org/simple/selenium),联网的话直接使用pip安装,命令进入 python的scripts目录,执行:pip install -U selenium;没联网的话,解压缩selenium- 2.28.0.tar.gz. 把selenium整个文件夹放入Python27\Lib\dist-packages目录下,或下载后使用 python setup.py install命令安装(注意安装版本),使用locate dist-packages查找目录位置。

 

注意:python2.5的新包都放在site-packages目录下,但是在python2.6改放在dist-packages目录下,site-packages不再是默认的目录,Ubuntu10.04是python2.6的环境。


5、下载并安装java(http://www.java.com/zh_CN/)。


6、 下载selenium的服务端(http://selenium.googlecode.com/files/selenium-server- standalone-2.28.0.jar),在selenium-server-standalone-2.28.0.jar目录下使用命令 java -jar selenium-server-standalone-2.28.0.jar启动(如果打不开,查看是否端口被占 用:netstat -aon|findstr 4444)。


7、打开python的idle,运行如下脚本,看运行是否成功。
# coding=gbk
from selenium import webdriver

browser = webdriver.Firefox() # 打开火狐浏览器
browser.get("http://www.baidu.com") # 登录百度首页


8、 要想支持IE,下载IEDriverServer(http://code.google.com/p/selenium/downloads /detail?name=IEDriverServer_Win32_2.28.0.zip),解压缩之后,将exe程序放在安装python的根目录 下。


9、运行过程中如果出现 WebDriverException: Message: u'Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.' 这 个错误,更改IE的internet选项->安全,将Internet/本地Internet/受信任的站定/受限制的站点中的启用保护模式全部去 掉勾,或者全部勾上。下面是一个IE的例子,打开百度,自动搜索selenium。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Ie()
driver.get("http://www.baidu.com")
elem = driver.find_element_by_name("wd")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics