第一步:安裝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/[email protected]/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/