Selenium学习笔记之001:Selenium+Eclipse+Python测试环境搭建 分类: Selenium 2015-07-12 16:36 13人阅读 评论(0) 收藏
第一步:安装Python:已配置过,此处省略。
第二步:安装Python的SetupTools:已配置过,此处省略。
第三步:安装Python的包管理工具 pip,有点类似SetupTools:已配置过,此处省略。
第四步:安装基于Python的Selenium包
打开DOS界面,进入到目录: C:\Python27\Scripts
然后敲入命令: pip install selenium或者pip install –U selenium(用后一个貌似报错,用前一个可安装。)
第五步:验证Selenium安装是否成功
在记事本中编写下面的代码:(保存为 pytest.py,然后双击直接运行即可!)
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://www.baidu.com")
assert "hao123" in browser.title
browser.close()
第六步:python的开发环境配置-Eclipse-PyDev插件安装:已配置过,此处省略。
第七步:执行Selenium实例
新建项目和py文件,如下图:# coding=utf-8
'''
Created on 2015-7-12
@author: Administrator
'''
from selenium import webdriver
if __name__ == "__main__":
driver = webdriver.Firefox()#启动浏览器
driver.implicitly_wait(30)#等待时间
driver.get("http://www.baidu.com")#打开的URL
print "Page title is :",driver.title #打印打开的网页标题
driver.quit() #退出浏览器
Win 7 64位系统环境下面搭建该测试环境,如果你是先安装python2.7之后再来安装setuptools和pip,那么你在用pip install selenium时可能会报错,比如提示你:Storing debug log for failure in C:\Users\XXX\pip\pip.log,所以需要在任意一个根目录下面新建一个register.py,该文件的具体内容,如下:
# # script to register Python 2.0 or later for use with win32all # and other extensions that require Python registry settings # # written by Joakim Loew for Secret Labs AB / PythonWare # # source: # http://www.pythonware.com/products/works/articles/regpy20.htm # # modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html import sys from _winreg import * # tweak as necessary version = sys.version[:3] installpath = sys.prefix regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version) installkey = "InstallPath" pythonkey = "PythonPath" pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % ( installpath, installpath, installpath ) def RegisterPy(): try: reg = OpenKey(HKEY_CURRENT_USER, regpath) except EnvironmentError as e: try: reg = CreateKey(HKEY_CURRENT_USER, regpath) SetValue(reg, installkey, REG_SZ, installpath) SetValue(reg, pythonkey, REG_SZ, pythonpath) CloseKey(reg) except: print "*** Unable to register!" return print "--- Python", version, "is now registered!" return if (QueryValue(reg, installkey) == installpath and QueryValue(reg, pythonkey) == pythonpath): CloseKey(reg) print "=== Python", version, "is already registered!" return CloseKey(reg) print "*** Unable to register!" print "*** You probably have another Python installation!" if __name__ == "__main__": RegisterPy()
建立好之后,在dos模式下,进入到对应的根目录下,输入以下命令:python register.py,系统就会自动运行该文件。然后再来运行pip来下载安装selenium,就不会报错了。
第八步:下载Selenium服务端http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html
下载jar包,并进入相应目录执行:java -jar selenium-server-standalone-xxx.jar(如果打不开,查看是否端口被占 用:netstat -aon|findstr 4444)。
在工程中运行官方代码:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
assert 0, "can't find seleniumhq"
browser.close()运行上面的代码,见图:
待补充安装各种类似的webdriver
参考文章:
http://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html
http://easonhan007.github.io/python/2013/05/07/setup-env/
版权声明:本文为博主原创文章,未经博主允许不得转载。
浙公网安备 33010602011771号