Selenium学习笔记之001:Selenium+Eclipse+Python测试环境搭建

来源:互联网 发布:中国数据挖掘会议 编辑:程序博客网 时间:2024/05/21 06:57

第一步:安装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 webdriverbrowser = webdriver.Firefox()browser.get("http://www.baidu.com")assert "hao123" in browser.titlebrowser.close()


运行成功后,如下图:


第六步:python的开发环境配置-Eclipse-PyDev插件安装:已配置过,此处省略。

第七步:执行Selenium实例

新建项目和py文件,如下图:




源代码:
# coding=utf-8'''Created on 2015-7-12@author: Administrator'''from selenium import webdriverif __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.htmlimport sysfrom _winreg import *# tweak as necessaryversion = sys.version[:3]installpath = sys.prefixregpath = "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!"returnprint "--- Python", version, "is now registered!"returnif (QueryValue(reg, installkey) == installpath andQueryValue(reg, pythonkey) == pythonpath):CloseKey(reg)print "=== Python", version, "is already registered!"returnCloseKey(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 webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysimport timebrowser = webdriver.Firefox() # Get local session of firefoxbrowser.get("http://www.yahoo.com") # Load pageassert "Yahoo!" in browser.titleelem = browser.find_element_by_name("p") # Find the query boxelem.send_keys("seleniumhq" + Keys.RETURN)time.sleep(0.2) # Let the page load, will be added to the APItry:    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/


http://selenium.googlecode.com/git/docs/api/py/index.html

0 0
原创粉丝点击