第一步:安裝Python:已配置過,此處省略。html
第二步:安裝Python的SetupTools:已配置過,此處省略。java
打開DOS界面,進入到目錄: C:\Python27\Scriptspython
而後敲入命令: pip install selenium或者pip install –U selenium(用後一個貌似報錯,用前一個可安裝。)git
第五步:驗證Selenium安裝是否成功github
在記事本中編寫下面的代碼:(保存爲 pytest.py,而後雙擊直接運行便可!)web
from selenium import webdriver browser = webdriver.Firefox() browser.get("http://www.baidu.com") assert "hao123" in browser.title browser.close()
# 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,該文件的具體內容,以下:api
# # 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()
第八步:下載Selenium服務端http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html瀏覽器
下載jar包,並進入相應目錄執行:java -jar selenium-server-standalone-xxx.jar(若是打不開,查看是否端口被佔 用:netstat -aon|findstr 4444)。session
在工程中運行官方代碼:python2.7
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/