今天无意间看到了splinter。
Splinter是一个使用Python开发的开源Web应用测试工具。它可以帮你实现自动浏览站点和与其进行交互。
Splinter对已有的自动化工具(如:Selenium、PhantomJS和zope.testbrowser)进行抽象,形成一个全新的上层应用API,它使为Web应用编写自动化测试脚本变的更容易。
依赖包
编辑
Splinter0.7.2依赖以下包:
Selenium(版本>=2.44.0)
Django(版本>=1.5.8,<1.7)
Flask(版本>=0.10)
lxml(版本>=2.3.6)
zope.testbrowser(版本>=4.0.4)
cssselect
代码示例
使用示例
1from splinter import Browser 2with Browser() as browser: 3 # Visit URL 4 url = "搜索引擎" 5 browser.visit(url) 6 browser.fill('q', 'splinter - python acceptance testing for web applications') 7 # Find and click the 'search' button 8 button = browser.find_by_name('btnG') 9 # Interact with elements 10 button.click() 11 if browser.is_text_present('splinter.readthedocs.org'): 12 print "Yes, the official website was found!" 13 else: 14 print "No, it wasn't found... We need to improve our SEO techniques"
与Selenium的比较
使用Splinter填充一个form的字段如下:
browser.fill('username', 'janedoe')
而使用Selenium需要:
1elem = browser.find_element.by_name('username') 2elem.send_keys('janedoe')
安装 Splinter
执行命令
pip install splinter

因为我之前已经安装了selenium以及chrome的驱动,所以这里就不再介绍,不懂得百度一下吧。
代码示例
1# FileName : SplinterDemo.py 2# Author : Adil 3# DateTime : 2018/1/16 20:59 4# SoftWare : PyCharm 5 6from splinter.browser import Browser 7browser = Browser(driver_name='chrome') 8browser.visit('https://www.hao123.com')
效果如下:
