下面是自己學習的時候找的資料,安裝后,遇到編碼問題,然后找的解決方法,都齊了,好記性不如爛筆頭,生命在于折騰
第一步:安裝Python
根據(jù)下面的地址,直接一鍵安裝,全部默認方式。
下載地址:http://www.python.org/ftp/python/2.7.5/python-2.7.5.msi
安裝到C:\Python27,設置Python環(huán)境變量,Path = E:\Python27;(前面安裝JDK時,已經(jīng)有了Path環(huán)境變量,這里直接加在最前面即可,注意Python27后面的‘;’)
第二步:安裝Python的SetupTools
其實SetupTools就是一個幫助你安裝第三方工具包的增強工具軟件,根據(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—有點類似SetupTools ,但是比它強大
利用第二步安裝的SetupTools進行安裝,打開DOS界面,進入到目錄:
C:\Python27\Scripts, 然后敲入命令: easy_install pip, 等待完成就OK。

第四步:安裝基于Python的Selenium包
打開DOS界面,進入到目錄: C:\Python27\Scripts
然后敲入命令: pip install selenium或者pip install –U selenium(用后一個貌似報錯,用前一個可安裝。)

安裝時可能會有一些警告,暫不用管,安裝完后如下,

第五步:驗證Selenium安裝是否成功
在記事本中編寫下面的代碼:(保存為 pytest.py,然后雙擊直接運行即可?。?br> from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://www.yahoo.com")
assert "Yahoo!" in browser.title
browser.close()
如果代碼運行成功,就表示Selenium安裝成功了! Very Good!
實際操作中此處報錯:
錯誤代碼如下: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)絡上找到答案:
- selenium 3.x開始,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(針對windows)即可。
作者:iceblue iceblue鏈接:https://www.zhihu.com/question/49568096/answer/119324584來源:知乎
第六步:python的開發(fā)環(huán)境配置-Eclipse-PyDev插件安裝
安裝PyDev插件的兩種安裝方法:
1、百度搜索PyDev 2.4.0.zip,下載后解壓,得到Plugins和Feature文件夾,復制兩文件夾到Eclipse目錄,覆蓋即可。
完成后重啟Eclipse,若在Eclipse菜單Help->About Eclipse->Installation Detail->Plug-ins,能看到PyDev組件,則表示安裝成功。

2、直接在Eclipse中選擇菜單:Help—Install New Software..—Add,輸入http://pydev.org/updates,下載并安裝。

配置 PyDev
安裝好 PyDev 之后,需要配置 Python/Jython 解釋器,配置過程很簡單。
在 Eclipse 菜單欄中,選擇 Window > Preferences > Pydev > Interpreter - Python,在這里配置 Python/解釋器,添加已安裝的解釋器。這里,Python 安裝在 C:\Python27 路徑下。單擊 New,選擇 Python 解釋器 python.exe,打開后顯示出一個包含很多復選框的窗口,選擇需要加入系統(tǒng) PYTHONPATH 的路徑,單擊 Ok。

第七步:執(zhí)行Selenium實例
下面,我們來創(chuàng)建一個python項目。
在 Eclipse 菜單欄中,選擇 File > New > Project > Pydev > Pydev Project,新建項目:PythonCase,單擊 Next。

完成后如下:

創(chuàng)建 Python 包和模塊
接下來,在剛創(chuàng)建的項目中開始創(chuàng)建 Python 包和模塊。
進入 Pydev 透視圖,在 Python Package Explorer 中,右鍵單擊 src,選擇 New->Pydev Package,輸入 Package 名稱Python27。
單擊 Finish,Python 包就創(chuàng)建好了,此時,自動生成init.py 文件,該文件不包含任何內(nèi)容。
注意:
如果在創(chuàng)建項目的時候沒有選中“Create default src folder and add it to the pythonpath”復選框,則需要通過 File > New > Other > Source Folder 手動創(chuàng)建一個源代碼文件夾src。
創(chuàng)建完 Pydev Package 后,右鍵單擊創(chuàng)建的包,選擇 New->Pydev Module,輸入模塊名稱PythonCase1.py Finish。這樣,Python 模塊就建成了。

修改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í)行腳本
運行Run_selenium.bat,啟動Selenium RC服務器。右擊PythonCase1.py,Run As->Python Run,執(zhí)行成功結(jié)果如下:

注意,如果有編碼問題:可以用如下方法解決:
Python 2.7安裝setuptools時的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,確切的說是Python安裝目錄下的Lib/mimetypes.py下的BUG,解決方案見地址:http://bugs.python.org/review/9291/diff/1663/Lib/mimetypes.py
如果想省事可以直接下載附件內(nèi)的mimetypes.py,已經(jīng)按照上述網(wǎng)址修改完畢,測試后可以正常運行。點此下載:mimetypes.py
記得將源文件備份哦,還有記得將mimetypes.pyc清除