搭建eclipse+python+selenium測試環境

經過幾天的糾結之後,終於在今天把該環境搭建起來了,在這裏要特別感謝深圳-乙醇老師的幫助
  搭建環境:
  系統環境:Win7 64位;
  JDK版本:java version “1.6.0_45″
  eclipse版本:4.2.0
  下面就給大家介紹一下詳細的步驟:
  (1) 下載一個active-python安裝軟件(該軟件已經包含了python2.7和setuptools),默認安裝好之後,對應的python目錄裏面就已經存在Scripts文件夾了,十分方便
  (2) 添加Path,比如:C:\Python27;C:\Python27\Scripts;(最好添加當前用戶下面的path,避免破壞其他用戶的系統環境)
  (3) 利用pip安裝selenium,具體做法如下:
  a. 下載和安裝一個帆檣軟件,這裏給大家介紹自由門,該軟件無需安裝,只需要運行起exe文件,就可以,很方便快捷
  b. 進入dos模式,切換路徑到C:\Python27\Scripts,然後輸入命令pip install selenium,系統就會自動下載和安裝selenium
  (4) 打開eclipse,安裝PyDev插件,具體操作如下:
  a. 直接在Eclipse中選擇菜單:Help—Install New Software..—Add,輸入http://pydev.org/updates,下載並安裝。
  b. 完成後重啓Eclipse,在Eclipse菜單Help->About Eclipse->Installation Detail->Plug-ins,若能看到PyDev組件,則表示安裝成功
  
  
(5) 需要配置Python解釋器,具體操作如下:
  在 Eclipse 菜單欄中,選擇 Window > Preferences > Pydev > Interpreter – Python。單擊 New,選擇 Python 解釋器 python.exe,點擊ok之後,就能添加你需要的插件內容
  (6) 測試是否配置成功
  a. 新建一個python項目,操作步驟可見截圖
  
  b. 新建一個python module,輸入以下內容
#-*- conding=utf-8 -*-
from selenium import webdriver
if __name__ == "__main__":
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get("http://www.google.com.hk")
print 'Page title is:',driver.title
driver.quit()
  c.如果通過firefox瀏覽器打開了google界面,那麼表明配置成功
  其中需要提示一下:
  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,就不會報錯了,報錯的童鞋可以試試,嘿嘿~~
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章