1. 程式人生 > >selenium win7+selenium2.0+python環境搭建

selenium win7+selenium2.0+python環境搭建

步驟1:下載python

擔心最新版的支援不太好,這裡我下載的是python2.7(selenium之前不支援python3.x)

步驟2:安裝python

下載好後直接安裝,安裝完後,記得把python的安裝路徑新增系統環境變數Path中(這個很簡單,不懂的請自己百度)。

步驟3:下載setuptools

我win732位系統,下載的是setuptools-0.6c11.win32-py2.7。setuptools是一個幫助你安裝python包的第三方工具,下載好後直接安裝即可(pip依賴於setuptools的安裝)

步驟4:安裝pip工具

cmd開啟控制檯,cd命令進入到E:Python2\Scripts>,執行 easy_installpip, 等待完成即可

C:\Users\laifuyu>cd /d "d:\ProgramFiles\Python27\Scripts"

d:\Program Files\Python27\Scripts> easy_install.exepip

附:easy_install使用說明

安裝套件:

easy_install PackageName

更新套件:

easy_install -U PackageName

移除套件:

easy_install -m PackageName

顯示說明:

easy_install --showhelp

備註:如果網路被牆或者沒連網,則上述方法安裝失敗,這種情況下,必須手動安裝。

下載得到piptar.gz檔案,這裡我用的是pip-1.5.4.tar,解壓,然後到windows的cmd中,進入解壓目錄執行setup.pyinstall

步驟5:安裝 Selenium Client Drivers

以下是官方說明:

In order to create scripts that interact with the SeleniumServer (Selenium RC, Selenium Remote Webdriver) or create localSelenium WebDriver script you need to make use of language-specificclient drivers. These languages include both 1.x and 2.x styleclients.

輸入pip installselenium 或者 pipinstall –Uselenium後回車,等待下載並安裝,如果無錯誤即安裝成功。

C:\Users\laifuyu>cd /d "d:\ProgramFiles\Python27\Scripts"

d:\Program Files\Python27\Scripts>pip.exe installselenium

說明:如果網路被牆或者非連網狀態,那麼上述安裝無效,必須手動安裝,步驟如下:

一,下載selenium包

下載地址:https://pypi.python.org/pypi/selenium

我下載的是selenium-2.40.0.tar.gz

手動下載selenium的安裝包後,右鍵解壓縮,,保險起見,解壓後把整個目錄放到C:\Python27\Lib\site-packages下,開啟CMD視窗,進入解壓後的目錄,執行命令  setup.pyinstall

selenium <wbr>win7+selenium2.0+python環境搭建

安裝完後可以刪除解壓縮後的檔案

步驟6:安裝JAVA JDK並配置環境變數

這裡我安裝的是jdk-6u14-windows-i586 Java SEDevelopment Kit 6u14 for Windows, Multi-language

環境變數的配置 參考“windows xp或Win7下配置JDK環境變數.doc“文件

步驟7.下載Eclipse

下載後解壓可用,無需安裝,我下載的是標準版eclipse-standard-kepler-SR2-win32

步驟8.在Eclipse搭建python的開發環境

有以下8.1,8.2兩種方法

8.1,下載PyDev2.5.0.zip後解壓(經過測試,PyDev 2.5.0可以,3.3.0不支援)

,得到Plugins和Feature資料夾,複製兩資料夾中的內容分別放到到Eclipse目錄下對應目錄中,然後重啟Eclipse。若在Eclipse選單Help->AboutEclipse->Installation Detail->Plug-ins,能看到PyDev元件,則表示安裝成功。

selenium <wbr>win7+selenium2.0+python環境搭建

8.2, 直接在Eclipse中選擇選單:Help—Install NewSoftware-Add,輸入http://pydev.org/updates,點選OK下載。

selenium <wbr>win7+selenium2.0+python環境搭建

然後選中下載好的PyDev軟體

selenium <wbr>win7+selenium2.0+python環境搭建

然後一路Next,進入安裝路徑選擇介面,使用預設設定,接受許可協議,然後Finish。Eclipse將下載 PyDev,可以從 Eclipse工作列中看到下載的進度。PyDev安裝好後,需要重啟Eclipse。

selenium <wbr>win7+selenium2.0+python環境搭建

安裝過程中可能出現上述,,

可能原因一:需要代理上網

解決方法:給eclipse設定代理

預設的Eclipse是不用代理上網,但在一些公司的區域網,需要使用代理上網,

因而需要手工設定eclipse的上網設定

window-->preferences-->general-->networkconnections

選中 manual proxyconfiguration: 依次填入http proxy ,port就ok了。

另外,如果代理需要用帳號和密碼就需要選中Enable proxy authentication,

然後填上 username 及password  

selenium <wbr>win7+selenium2.0+python環境搭建

步驟9.配置PyDev

PyDev安裝好之後,需要配置直譯器。在 Eclipse選單欄中,選擇Window >Preferences > Pydev > Interpreter – Python>New,輸入名字和Python.exe所在安裝路徑,新增已安裝的直譯器

selenium <wbr>win7+selenium2.0+python環境搭建

點選Ok,跳出一個有很多複選框的視窗,選擇需要加入SYSTEMpythonpath的選項

selenium <wbr>win7+selenium2.0+python環境搭建

一般可以採用預設設定,點選Ok

selenium <wbr>win7+selenium2.0+python環境搭建

 點選OK,儲存配置

步驟10.執行Selenium例項

下面,我們來建立一個python專案。

在 Eclipse選單欄中,選擇 File > New >Project > Pydev > Pydev Project,新建專案:PyCase,單擊 Next。

selenium <wbr>win7+selenium2.0+python環境搭建

注意:如果在建立專案的時候沒有選中“Createdefault src folder and add it to the pythonpath”複選框,則需要通過 File > New > Other> Source Folder 手動建立一個原始碼資料夾src。

點選finish,如下

selenium <wbr>win7+selenium2.0+python環境搭建


 步驟11.建立Python 包和模組

接下來,在剛建立的專案中開始建立Python包和模組。

進入 Pydev透檢視,在 Python PackageExplorer 中,右鍵單擊src,選擇New->Pydev Package,輸入 Package 名稱Py27。


selenium <wbr>win7+selenium2.0+python環境搭建

單擊Finish,Python 包就建立好了,此時,自動生成__init__.py檔案,該檔案不包含任何內容。

selenium <wbr>win7+selenium2.0+python環境搭建

建立完 Pydev Package後,右鍵單擊建立的包Py27,選擇 New->PydevModule,輸入模組名稱PyCase1.pyFinish。這樣,Python 模組就建成了

selenium <wbr>win7+selenium2.0+python環境搭建

 selenium <wbr>win7+selenium2.0+python環境搭建

步驟12.修改PyCase1.py的內容如下

# -*- coding: utf-8-*-

fromselenium import webdriver

importtime

if__name__ == "__main__":

   driver = webdriver.Ie()

   driver.get('http://www.baidu.com')

   driver.implicitly_wait(20)  #

   driver.find_element_by_id('kw1').send_keys(u'授客')

   driver.find_element_by_id('su1').click()

   time.sleep(5)  

   driver.quit()

步驟13.新增瀏覽器驅動webdriver支援

這裡我用的是IEDriverServer_Win32_2.40.0.zip,下載後解壓,把IEDriverServer.exe放在python安裝目錄,與python.exe在同一目錄下。

備註:如過是其它瀏覽器也是則換成其它瀏覽器驅動

步驟14.執行指令碼看效果

右擊PythonCase1.py,Run As->PythonRun,,會彈出ie瀏覽器並自動輸入查詢條件,執行。

 .0 python 環境搭建 eclipse

備註:執行過程中如果出現WebDriverException:Message: u'Unexpected error launching Internet Explorer. ProtectedMode settings are not the same for all zones. Enable Protected Modemust be set to the same value (enabled or disabled) for allzones.' 這個錯誤,更改IE的internet選項->安全,將Internet/本地Internet/受信任的站定/受限制的站點中的啟用保護模式全部去掉勾,或者全部勾上。