splinter介紹
Splinter是一個(gè)使用Python測(cè)試Web應(yīng)用程序的開源工具,可以自動(dòng)化瀏覽器操作,例如訪問URL和與它們的項(xiàng)進(jìn)行交互。例如,我們使用百度引擎搜索內(nèi)容,需要再搜索框內(nèi)輸入關(guān)鍵字,再按百度一下
即可以搜索想要的內(nèi)容,使用Splinter可以使用pyhton腳本來(lái)實(shí)現(xiàn)上述過(guò)程。
Splinter安裝
Splinter的使用需要依賴python環(huán)境,因此首先需要裝python(python安裝可以直接安裝anaconda
集成環(huán)境,網(wǎng)上一搜教程很多~),并且python版本需要是2.7+;以下是Splinter的官網(wǎng)說(shuō)明:
In order to install Splinter, make sure Python is installed. Note: only Python 2.7+ is supported.
Splinter安裝
Splinter安裝,官網(wǎng)提供了兩種版本安裝,一般使用穩(wěn)定版本即可:
pip install splinter # pip工具首先得安裝,如果安裝anaconda則會(huì)自動(dòng)安裝pip
驅(qū)動(dòng)安裝
要使用splinter訪問瀏覽器,還需要安裝對(duì)應(yīng)的瀏覽器驅(qū)動(dòng),這里以chrome為例,由于chrome WebDriver
依賴于Selenium2
,最終需要安裝兩個(gè):即Selenium2
和chromedriver
。
1. Selenium2直接通過(guò)pip安裝:
2. 對(duì)于chromedriver,首先查看瀏覽器版本,在chrome瀏覽器訪問:chrome://version/
。

然后訪問http://chromedriver.storage.googleapis.com/index.html
,找到對(duì)應(yīng)的版本下載即可。

下載解壓后,會(huì)得到一個(gè)chromedriver.exe
文件,按照官網(wǎng)的說(shuō)法,需要將其配置環(huán)境變量。簡(jiǎn)單的做法,直接將chromedriver.exe
文件放在python安裝的根目錄(即和python.exe
放在同一個(gè)目錄===這是因?yàn)?code>python.exe所在的目錄肯定配置了環(huán)境變量)。到這里,環(huán)境配置已經(jīng)OK了,接著就是寫python腳本測(cè)試了~
python腳本測(cè)試Splinter
from splinter import Browser
from time import sleep
browser = Browser('chrome') # 創(chuàng)建瀏覽器實(shí)例
browser.visit('https://www.baidu.com') # 訪問baidu
# 將關(guān)鍵詞填入搜索框 通過(guò)wd這個(gè)名字找到對(duì)應(yīng)的Elements
browser.fill('wd', 'splinter - python acceptance testing for web applications')
browser.find_by_id('su').click() # 通過(guò)id找到點(diǎn)擊按鈕,并點(diǎn)擊
if browser.is_text_present('splinter.readthedocs.io'): # 對(duì)響應(yīng)結(jié)果進(jìn)行處理
print("Yes, the official website was found!")
else:
print("No, it wasn't found... We need to improve our SEO techniques")
sleep(10)
browser.quit() # 關(guān)閉瀏覽器
其中,browser = Browser('chrome')
的'chrome'
參數(shù)是必須的,如果不指定的話,默認(rèn)選用火狐瀏覽器,詳見官網(wǎng)說(shuō)明。

結(jié)果:


到此這篇關(guān)于Python測(cè)試開源工具splinter安裝與使用教程的文章就介紹到這了,更多相關(guān)python splinter安裝與使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 基于Python3.6+splinter實(shí)現(xiàn)自動(dòng)搶火車票
- python+splinter自動(dòng)刷新?lián)屍惫δ?/li>
- Python利用splinter實(shí)現(xiàn)瀏覽器自動(dòng)化操作方法
- 使用Python+Splinter自動(dòng)刷新?lián)?2306火車票
- Python自動(dòng)化測(cè)試工具Splinter簡(jiǎn)介和使用實(shí)例