(轉(zhuǎn)載)windows下搭建Selenium+Eclipse+Python環(huán)境

下面是自己學(xué)習(xí)的時(shí)候找的資料,安裝后,遇到編碼問(wèn)題,然后找的解決方法,都齊了,好記性不如爛筆頭,生命在于折騰
第一步:安裝Python
根據(jù)下面的地址,直接一鍵安裝,全部默認(rèn)方式。
下載地址:http://www.python.org/ftp/python/2.7.5/python-2.7.5.msi
安裝到C:\Python27,設(shè)置Python環(huán)境變量,Path = E:\Python27;(前面安裝JDK時(shí),已經(jīng)有了Path環(huán)境變量,這里直接加在最前面即可,注意Python27后面的‘;’)
第二步:安裝Python的SetupTools
其實(shí)SetupTools就是一個(gè)幫助你安裝第三方工具包的增強(qiáng)工具軟件,根據(jù)下面的地址下載,然后按下一步一鍵安裝。
setuptools-0.6c11.win32-py2.7.exe.exes
下載地址:
http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11.win32-py2.7.exe#md5=57e1e64f6b7c7f1d2eddfc9746bbaf20
第三步:安裝Python的包管理工具 pip—有點(diǎn)類似SetupTools ,但是比它強(qiáng)大
利用第二步安裝的SetupTools進(jìn)行安裝,打開(kāi)DOS界面,進(jìn)入到目錄:
C:\Python27\Scripts, 然后敲入命令: easy_install pip, 等待完成就OK。

(轉(zhuǎn)載)windows下搭建Selenium+Eclipse+Python環(huán)境 Tester 第1張

第四步:安裝基于Python的Selenium包
打開(kāi)DOS界面,進(jìn)入到目錄: C:\Python27\Scripts
然后敲入命令: pip install selenium或者pip install –U selenium(用后一個(gè)貌似報(bào)錯(cuò),用前一個(gè)可安裝。)
(轉(zhuǎn)載)windows下搭建Selenium+Eclipse+Python環(huán)境 Tester 第2張

安裝時(shí)可能會(huì)有一些警告,暫不用管,安裝完后如下,
(轉(zhuǎn)載)windows下搭建Selenium+Eclipse+Python環(huán)境 Tester 第3張

第五步:驗(yàn)證Selenium安裝是否成功
在記事本中編寫下面的代碼:(保存為 pytest.py,然后雙擊直接運(yùn)行即可!)
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://www.yahoo.com")
assert "Yahoo!" in browser.title
browser.close()
如果代碼運(yùn)行成功,就表示Selenium安裝成功了! Very Good!

實(shí)際操作中此處報(bào)錯(cuò):

錯(cuò)誤代碼如下:File "/usr/local/lib/python2.7/dist-packages/selenium-3.0.0b2-py2.7.egg/selenium/webdriver/firefox/webdriver.py", line 65, in init self.service.start() File "/usr/local/lib/python2.7/dist-packages/selenium-3.0.0b2-py2.7.egg/selenium/webdriver/common/service.py", line 71, in start os.path.basename(self.path), self.start_error_message)selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.del of <selenium.webdriver.firefox.service.Service object at 0x7f753ad53390>> ignored

網(wǎng)絡(luò)上找到答案:

  1. selenium 3.x開(kāi)始,webdriver/firefox/webdriver.py的init中,executable_path="geckodriver";而2.x是executable_path="wires"2. firefox 47以上版本,需要下載第三方driver,即geckodriver;在http://docs.seleniumhq.org/download/**的Third Party Drivers, Bindings, and Plugins下面找到Mozilla GeckoDriver**,下載到任意電腦任意目錄,解壓后將該路徑加入到PC的path(針對(duì)windows)即可。
    作者:iceblue iceblue鏈接:https://www.zhihu.com/question/49568096/answer/119324584來(lái)源:知乎

第六步:python的開(kāi)發(fā)環(huán)境配置-Eclipse-PyDev插件安裝
  安裝PyDev插件的兩種安裝方法:
  1、百度搜索PyDev 2.4.0.zip,下載后解壓,得到Plugins和Feature文件夾,復(fù)制兩文件夾到Eclipse目錄,覆蓋即可。
完成后重啟Eclipse,若在Eclipse菜單Help->About Eclipse->Installation Detail->Plug-ins,能看到PyDev組件,則表示安裝成功。

(轉(zhuǎn)載)windows下搭建Selenium+Eclipse+Python環(huán)境 Tester 第4張

  2、直接在Eclipse中選擇菜單:Help—Install New Software..—Add,輸入http://pydev.org/updates,下載并安裝。
(轉(zhuǎn)載)windows下搭建Selenium+Eclipse+Python環(huán)境 Tester 第5張

  配置 PyDev
安裝好 PyDev 之后,需要配置 Python/Jython 解釋器,配置過(guò)程很簡(jiǎn)單。
在 Eclipse 菜單欄中,選擇 Window > Preferences > Pydev > Interpreter - Python,在這里配置 Python/解釋器,添加已安裝的解釋器。這里,Python 安裝在 C:\Python27 路徑下。單擊 New,選擇 Python 解釋器 python.exe,打開(kāi)后顯示出一個(gè)包含很多復(fù)選框的窗口,選擇需要加入系統(tǒng) PYTHONPATH 的路徑,單擊 Ok。
(轉(zhuǎn)載)windows下搭建Selenium+Eclipse+Python環(huán)境 Tester 第6張

第七步:執(zhí)行Selenium實(shí)例
下面,我們來(lái)創(chuàng)建一個(gè)python項(xiàng)目。
在 Eclipse 菜單欄中,選擇 File > New > Project > Pydev > Pydev Project,新建項(xiàng)目:PythonCase,單擊 Next。
(轉(zhuǎn)載)windows下搭建Selenium+Eclipse+Python環(huán)境 Tester 第7張

完成后如下:
(轉(zhuǎn)載)windows下搭建Selenium+Eclipse+Python環(huán)境 Tester 第8張

創(chuàng)建 Python 包和模塊
接下來(lái),在剛創(chuàng)建的項(xiàng)目中開(kāi)始創(chuàng)建 Python 包和模塊。
進(jìn)入 Pydev 透視圖,在 Python Package Explorer 中,右鍵單擊 src,選擇 New->Pydev Package,輸入 Package 名稱Python27。
單擊 Finish,Python 包就創(chuàng)建好了,此時(shí),自動(dòng)生成init.py 文件,該文件不包含任何內(nèi)容。
注意:
如果在創(chuàng)建項(xiàng)目的時(shí)候沒(méi)有選中“Create default src folder and add it to the pythonpath”復(fù)選框,則需要通過(guò) File > New > Other > Source Folder 手動(dòng)創(chuàng)建一個(gè)源代碼文件夾src。
創(chuàng)建完 Pydev Package 后,右鍵單擊創(chuàng)建的包,選擇 New->Pydev Module,輸入模塊名稱PythonCase1.py Finish。這樣,Python 模塊就建成了。
(轉(zhuǎn)載)windows下搭建Selenium+Eclipse+Python環(huán)境 Tester 第9張

修改PythonCase1.py內(nèi)容如下:

-- conding=utf-8 --

from selenium import webdriver
if name == "main":
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get("http://www.google.com.hk")
driver.find_element_by_name("q").send_keys("hello Selenium!")
driver.find_element_by_name("q").submit()
print 'Page title is:',driver.title
driver.quit()
執(zhí)行腳本
運(yùn)行Run_selenium.bat,啟動(dòng)Selenium RC服務(wù)器。右擊PythonCase1.py,Run As->Python Run,執(zhí)行成功結(jié)果如下:

(轉(zhuǎn)載)windows下搭建Selenium+Eclipse+Python環(huán)境 Tester 第10張

注意,如果有編碼問(wèn)題:可以用如下方法解決:

Python 2.7安裝setuptools時(shí)的UnicodeDecodeError解決辦法
[python] view plain copy

Traceback (most recent call last):

File "c:\docume1\admini1\locals~1\temp\tmpbg7qjrpycharm-management\setuptools-1.1.5\setup.py", line 17, in <module>

exec(init_file.read(), command_ns)  

File "<string>", line 8, in <module>

File "c:\docume1\admini1\locals~1\temp\tmpbg7qjrpycharm-management\setuptools-1.1.5\setuptools_init_.py", line 11, in <module>

from setuptools.extension import Extension  

File "c:\docume1\admini1\locals~1\temp\tmpbg7qjrpycharm-management\setuptools-1.1.5\setuptools\extension.py", line 5, in <module>

from setuptools.dist import _get_unpatched  

File "c:\docume1\admini1\locals~1\temp\tmpbg7qjrpycharm-management\setuptools-1.1.5\setuptools\dist.py", line 15, in <module>

from setuptools.compat import numeric_types, basestring  

File "c:\docume1\admini1\locals~1\temp\tmpbg7qjrpycharm-management\setuptools-1.1.5\setuptools\compat.py", line 19, in <module>

from SimpleHTTPServer import SimpleHTTPRequestHandler  

File "C:\Python27\lib\SimpleHTTPServer.py", line 27, in <module>

class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):  

File "C:\Python27\lib\SimpleHTTPServer.py", line 208, in SimpleHTTPRequestHandler

mimetypes.init() # try to read system mime.types  

File "C:\Python27\lib\mimetypes.py", line 358, in init

db.read_windows_registry()  

File "C:\Python27\lib\mimetypes.py", line 258, in read_windows_registry

for subkeyname in enum_types(hkcr):  

File "C:\Python27\lib\mimetypes.py", line 249, in enum_types

ctype = ctype.encode(default_encoding) # omit in 3.x!  

UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 9: ordinal not in range(128)

排查半天未果,GOOGLE之,發(fā)現(xiàn)是Python 2.7的Bug,確切的說(shuō)是Python安裝目錄下的Lib/mimetypes.py下的BUG,解決方案見(jiàn)地址:http://bugs.python.org/review/9291/diff/1663/Lib/mimetypes.py
如果想省事可以直接下載附件內(nèi)的mimetypes.py,已經(jīng)按照上述網(wǎng)址修改完畢,測(cè)試后可以正常運(yùn)行。點(diǎn)此下載:mimetypes.py
記得將源文件備份哦,還有記得將mimetypes.pyc清除

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容