STEP0: 搭環境最頭疼的仍是各工具,軟件版本的兼容性問題。暫不羅列遇到的問題了,直接把本身的工具版本貼出來:html
IEDriverServer_Win32_2.38.0.zip 解壓到能夠運行 cmd的任何目錄python
Firefox 24.0 連接 (46.0.1版本)其webdriver在selenium的安裝目錄下如:git
C:\Python27\Lib\site-packages\selenium\webdriver\firefox\webdriver.xpigithub
安裝時直接將其拖入firefox下便可安裝。web
Selenium 2.41.0 使用pip安裝chrome
C:\Users\Administrator>pip list pip (1.4.1) pypm (1.4.0) pythonselect (1.3) pywin32 (218.3) selenium (2.41.0) setuptools (1.1) virtualenv (1.10.1) wsgiref (0.1.2)
Python2.7 active pythonapi
STEP1:安裝pip 瀏覽器
下載,並運行 https://raw.github.com/pypa/pip/master/contrib/get-pip.py安全
STEP2:安裝active python 2.7工具
http://www.activestate.com/activepython
STEP3:安裝selenium
pip install selenium
注:安裝如出現編碼異常,請在C:\Python27\Lib\mimetypes.py頭部加入
參考:http://bbs.csdn.net/topics/390654855
# begin added by semon for 'UnicodeDecodeError' if sys.getdefaultencoding() != 'gbk': reload(sys) sys.setdefaultencoding('gbk') # end added by semon
STEP4:安裝firefox driver 見STEP0
STEP5:安裝ie driver 見STPE0
IE要作設置: Ineternet 選項->安全 ->將全部區域重置爲默認級別->取消四個區域的「啓用保護模式.....」
STEP6:安裝chrome driver
chromedriver: 連接 選最高版本,筆者選 2.9版本 window 32bit版本
chrome版本:34.0.1847.116 m
STEP7:測試下
from selenium import webdriver print '------------ie------------' driver_ie = webdriver.Ie() driver_ie.get('http://www.baidu.com') print driver_ie.title print '------------firefox------------' driver_ff = webdriver.Firefox() driver_ff.get('http://www.baidu.com') print driver_ff.title print '------------chrome--------------' driver_chrome = webdriver.Chrome("C:\\Program Files\\Google\\chrome\\Application\\chromedriver.exe") driver_chrome .get('http://www.baidu.com') print driver_chrome .title driver_ff.quit() driver_ie.quit() driver_chrome.quit()
輸出截圖:
補上chrome截圖
Refer:
須要懂些關於js和html方面的知識,由於driver須要常常用到這些方法:
element = driver.find_element_by_id("id_xxx")
element.send_keys("文本框內容")
elementBtn.submit() #提交按鈕
http://join12.blog.51cto.com/1194785/1310881
http://my.oschina.net/dyhunter/blog/94090
踩坑:對於chrome而言,下載到正確的driver很重要,連接:
https://sites.google.com/a/chromium.org/chromedriver/downloads
踩坑:對IE而言,要下載 IEDriverServer 並設置瀏覽器
http://join12.blog.51cto.com/1194785/1310881