[2B][尝鲜] selenium webdriver

爱⌒轻易说出口 提交于 2019-12-09 15:05:05

STEP0: 搭环境最头疼的还是各工具,软件版本的兼容性问题。暂不罗列遇到的问题了,直接把自己的工具版本贴出来:

  • IEDriverServer_Win32_2.38.0.zip 解压到可以运行 cmd的任何目录

  • Firefox 24.0 链接 (46.0.1版本)其webdriver在selenium的安装目录下如:

        C:\Python27\Lib\site-packages\selenium\webdriver\firefox\webdriver.xpi

        安装时直接将其拖入firefox下即可安装。

  • Selenium 2.41.0 使用pip安装

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 python

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

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!