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