python 通過(guò)xml獲取測(cè)試節(jié)點(diǎn)和屬性的實(shí)例
寫(xiě)在前面:通過(guò)xml獲取測(cè)試數(shù)據(jù),主要是為了使數(shù)據(jù)參數(shù)化。測(cè)試腳本和測(cè)試數(shù)據(jù)分離,使得腳本清晰容易維護(hù),方便排查問(wèn)題。
XML:可擴(kuò)展的標(biāo)記語(yǔ)言,是一種用于標(biāo)記電子文件使其具有結(jié)構(gòu)行的標(biāo)記語(yǔ)言。
自動(dòng)化測(cè)試中的使用場(chǎng)景:
1. 經(jīng)常變動(dòng)的測(cè)試數(shù)據(jù);
2. 數(shù)據(jù)量大,不方便放在腳本中;
3. 數(shù)據(jù)作用于多個(gè)地方;
4. 相同測(cè)試用例,可以使用不同的數(shù)據(jù);
5. 例:不穩(wěn)定,后續(xù)改動(dòng)較多功能;容易出錯(cuò)的功能
XML特征
• 具有自我描述性,本身不做任何事情
• 聲明部分 <?xml version="1.0" encoding="UTF-8"?>
• 有開(kāi)就有閉,成對(duì)出現(xiàn) <data></data>
• 可以有屬性值 <data type='demo'></data>
• xml用于傳輸數(shù)據(jù) <data type='demo'> message </data>
• 可以嵌入子標(biāo)簽 <data type='demo'><text>message</text></data>
python獲取xml文件方法集合
引入模塊處理xml文件
from xml.dom.minidom import parse
打開(kāi)xml文檔,
DOMTree = xml.dom.minidom.parse(data_path)
根據(jù)xml文檔,得到文檔元素的對(duì)象
data = DOMTree.documentElement
獲取節(jié)點(diǎn)列表
nodelist = data.getElementsByTagName(大類(lèi)名稱(chēng))
獲取第一個(gè)節(jié)點(diǎn)的子節(jié)點(diǎn)列表
childlist = nodelist[0].childNodes
獲取XML節(jié)點(diǎn)屬性值
node.getAttribute(AttributeName)
獲取XML節(jié)點(diǎn)對(duì)象集合
node.getElementsByTagName(TagName)
返回子節(jié)點(diǎn)列表
node.childNodes
獲取XML節(jié)點(diǎn)值
node.childNodes[index].nodeValue
訪問(wèn)第一個(gè)節(jié)點(diǎn)
node.firstChild ,等價(jià)于pagexml.childNodes[0]
訪問(wèn)元素屬性 例如:
Node.attributes["id"] a.name #就是上面的 "id" a.value #屬性的值
以下為具體的demo內(nèi)容:
方法調(diào)用
#方法調(diào)用
text = get_data_vaule('account','type','createText','text')
print text
#結(jié)果
test data
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<data>
<account type='createText'>
<text>test data</text>
</account>
<account type='email'>
<username>11111@qq.com</username>
<password>11111111</password>
</account>
</data>
下面方法相對(duì)應(yīng)取數(shù)據(jù)的格式,# style = xml中的大類(lèi) ; typename = 細(xì)分屬性; typevalue = 細(xì)分屬性的值; valuename = xml文件,需要獲取的值的tag;
如果需要獲取相對(duì)應(yīng)的上面XML文件中
“test data”的值,那么style = data ; typename = type; typevalue = createText; valuename = text “11111@qq.com”的值,那么style = data ; typename = type; typevalue = email; valuename = username
方法文件
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import unittest
import os
import time
from xml.dom.minidom import parse
import xml.dom.minidom
#獲取xml文件地址
path = os.path.abspath('.')
data_path = os.path.join(path,'features/data/data.xml') #獲取xml文件地址
DOMTree = xml.dom.minidom.parse(data_path)
data = DOMTree.documentElement
def get_attrvalue(node, attrname):
return node.getAttribute(attrname)
# style = xml中的大類(lèi) ; typename = 細(xì)分屬性; typevalue = 細(xì)分屬性的值; valuename = xml文件,需要獲取的值的tag;
def get_data_vaule(style, typename, typevalue, valuename):
nodelist = data.getElementsByTagName(style)
for node in nodelist:
if typevalue == node.getAttribute(typename):
node_name = node.getElementsByTagName(valuename)
value = node_name[0].childNodes[0].nodeValue
print value
return value
return
以上這篇python 通過(guò)xml獲取測(cè)試節(jié)點(diǎn)和屬性的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python實(shí)現(xiàn)針對(duì)給定單鏈表刪除指定節(jié)點(diǎn)的方法
- Python基于lxml模塊解析html獲取頁(yè)面內(nèi)所有葉子節(jié)點(diǎn)xpath路徑功能示例
- python+matplotlib繪制簡(jiǎn)單的海豚(頂點(diǎn)和節(jié)點(diǎn)的操作)
- Python算法之求n個(gè)節(jié)點(diǎn)不同二叉樹(shù)個(gè)數(shù)
- python xml.etree.ElementTree遍歷xml所有節(jié)點(diǎn)實(shí)例詳解
- Python selenium 父子、兄弟、相鄰節(jié)點(diǎn)定位方式詳解
- Python獲取任意xml節(jié)點(diǎn)值的方法
- python實(shí)現(xiàn)單鏈表中刪除倒數(shù)第K個(gè)節(jié)點(diǎn)的方法
相關(guān)文章
Python如何獲取實(shí)時(shí)股票信息的方法示例
本文主要介紹了Python如何獲取實(shí)時(shí)股票信息的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
python多線程http下載實(shí)現(xiàn)示例
python多線程http下載實(shí)現(xiàn)示例,大家參考使用吧2013-12-12
翻轉(zhuǎn)數(shù)列python實(shí)現(xiàn),求前n項(xiàng)和,并能輸出整個(gè)數(shù)列的案例
這篇文章主要介紹了翻轉(zhuǎn)數(shù)列python實(shí)現(xiàn),求前n項(xiàng)和,并能輸出整個(gè)數(shù)列的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
python訓(xùn)練數(shù)據(jù)時(shí)打亂訓(xùn)練數(shù)據(jù)與標(biāo)簽的兩種方法小結(jié)
今天小編就為大家分享一篇python訓(xùn)練數(shù)據(jù)時(shí)打亂訓(xùn)練數(shù)據(jù)與標(biāo)簽的兩種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Python學(xué)生成績(jī)管理系統(tǒng)簡(jiǎn)潔版
這篇文章主要為大家詳細(xì)介紹了簡(jiǎn)潔版的Python學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01

