Python splinter 環境搭建


今天無意間看到了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
 

代碼示例


使用示例

from splinter import Browser
with Browser() as browser:
    # Visit URL
    url = "搜索引擎"
    browser.visit(url)
    browser.fill('q', 'splinter - python acceptance testing for web applications')
    # Find and click the 'search' button
    button = browser.find_by_name('btnG')
    # Interact with elements
    button.click()
    if browser.is_text_present('splinter.readthedocs.org'):
        print "Yes, the official website was found!"
    else:
        print "No, it wasn't found... We need to improve our SEO techniques"

 

與Selenium的比較

使用Splinter填充一個form的字段如下:
browser.fill('username', 'janedoe')
 
而使用Selenium需要:
elem = browser.find_element.by_name('username')
elem.send_keys('janedoe')

 

安裝 Splinter 

執行命令 

pip install splinter

 

 因為我之前已經安裝了selenium以及chrome的驅動,所以這里就不再介紹,不懂得百度一下吧。

代碼示例

# FileName : SplinterDemo.py
# Author   : Adil
# DateTime : 2018/1/16 20:59
# SoftWare : PyCharm

from splinter.browser import Browser
browser = Browser(driver_name='chrome')
browser.visit('https://www.hao123.com')

 

 效果如下:

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM