Selenium2+python自動化20-Excle資料參數化

來源:互聯網
上載者:User

標籤:unittest   user   div   模組   免費   tar   介紹   utf-8   pac   

前言

問: Python 擷取到Excel一列值後怎麼用selenium錄製的指令碼中參數化,比如對登入使用者名稱和密碼如何做參數化?

答:可以使用xlrd讀取Excel的內容進行參數化。當然為了便於各位小夥伴們詳細的瞭解,小編一一介紹具體的方法。

一、編寫登入用例:

Step1:訪問http://www.effevo.com (打個廣告effevo是搜狗自研發的專案管理系統,完全免費,非常好用)
Step2:點擊頁面右上方的登入
Step3:輸入使用者名稱和密碼後登入
Step4:檢查右上方的頭像是否存在
實現代碼:

二、製作Excel檔案:

下一步,我們要對以上輸入的使用者名稱和密碼進行參數化,使得這些資料讀取自Excel檔案。我們將Excel檔案命名為data.xlsx,其中有兩列資料,第一列為passname,第二列為password。

三、安裝xlrd第三方庫:

Python讀取Excel檔案需要使用第三方的庫檔案xlrd,我們到python官網下載http://pypi.python.org/pypi/xlrd模組安裝。
1.下載xlrd-0.9.4.tar.gz
2.解壓該檔案。由於該檔案是用tar命令壓縮的,所以建議windows使用者用7zip解壓
3.命令列運行Setup.py檔案:setup.py install
4.提示安裝完畢後,我們就可以在Python指令碼中import xlrd了。
註:如果安裝沒有提示什麼,可直接把安裝檔案裡面的xlrd目錄複寫到D:\Python\Lib\site-packages目錄就可以用了

四、Excel資料參數化:

# encoding: utf-8
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
import unittest,time,xlrd
#import xdrlib ,sys
def open_excel(file= ‘login.xlsx‘):
try:
data = xlrd.open_workbook(file)
return data
except Exception,e:
print str(e)
#根據索引擷取Excel表格中的資料 參數:file:Excel檔案路徑 colnameindex:表頭列名所在行的所以 ,by_index:表的索引
def excel_table_byindex(file= ‘login.xlsx‘,colnameindex=0,by_index=0):
data = open_excel(file)
table = data.sheets()[by_index]
nrows = table.nrows #行數
colnames = table.row_values(colnameindex) #某一行資料
list =[]
for rownum in range(1,nrows):
row = table.row_values(rownum)
if row:
app = {}
for i in range(len(colnames)):
app[colnames[i]] = row[i]
list.append(app)
return list
def Login():
listdata = excel_table_byindex("C:\Users\hzy\Desktop\login.xlsx" , 0)
if (len(listdata) <= 0 ):
assert 0 , u"Excel資料異常"
for i in range(0 , len(listdata) ):
driver = webdriver.Firefox()
driver.get("http://192.168.0.251/user/user_login.aspx")
assert "user_login" in driver.title
#點擊登入按鈕
driver.find_element_by_id(‘txtusername‘).send_keys(listdata[i][‘txtusername‘])
driver.find_element_by_id(‘txtpassword‘).send_keys(listdata[i][‘txtpassword‘])
driver.find_element_by_id("userlogin").click()
time.sleep(2)
try:
elem = driver.find_element_by_xpath(".//*[@id=‘ee-header‘]/div/div/div/ul[2]/li/a/img")
except NoSuchElementException:
assert 0 , u"登入失敗,找不到右上方頭像"
driver.close()
if __name__ == ‘__main__‘:
Login()

 

如有錯誤或不合理的地方,請大家多多指教!

Selenium2+python自動化20-Excle資料參數化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.