欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 通過xml獲取測(cè)試節(jié)點(diǎn)和屬性的實(shí)例

 更新時(shí)間:2018年03月31日 11:50:10   作者:丟丟是一只喵  
下面小編就為大家分享一篇python 通過xml獲取測(cè)試節(jié)點(diǎn)和屬性的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

寫在前面:通過xml獲取測(cè)試數(shù)據(jù),主要是為了使數(shù)據(jù)參數(shù)化。測(cè)試腳本和測(cè)試數(shù)據(jù)分離,使得腳本清晰容易維護(hù),方便排查問題。

XML:可擴(kuò)展的標(biāo)記語言,是一種用于標(biāo)記電子文件使其具有結(jié)構(gòu)行的標(biāo)記語言。

自動(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"?>

• 有開就有閉,成對(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

打開xml文檔,

DOMTree = xml.dom.minidom.parse(data_path)

根據(jù)xml文檔,得到文檔元素的對(duì)象

data = DOMTree.documentElement

獲取節(jié)點(diǎn)列表

nodelist = data.getElementsByTagName(大類名稱)

獲取第一個(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

訪問第一個(gè)節(jié)點(diǎn)

node.firstChild ,等價(jià)于pagexml.childNodes[0]

訪問元素屬性 例如:

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中的大類 ; 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中的大類 ; 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 通過xml獲取測(cè)試節(jié)點(diǎn)和屬性的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實(shí)現(xiàn)自動(dòng)化處理Word文檔的方法詳解

    Python實(shí)現(xiàn)自動(dòng)化處理Word文檔的方法詳解

    本文主要介紹了如何使用Python實(shí)現(xiàn)Word文檔的自動(dòng)化處理,包括批量生成Word文檔、在Word文檔中批量進(jìn)行查找和替換、將Word文檔批量轉(zhuǎn)換成PDF等,希望對(duì)你有所幫助
    2022-08-08
  • php memcached的實(shí)例用法詳解

    php memcached的實(shí)例用法詳解

    在本篇文章里小編給大家整理了一篇關(guān)于php memcached的實(shí)例用法內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)參考下。
    2021-10-10
  • 精心整理總結(jié)的Python自動(dòng)化測(cè)試面試題

    精心整理總結(jié)的Python自動(dòng)化測(cè)試面試題

    簡(jiǎn)單來說,自動(dòng)化測(cè)試框架包含了所有的測(cè)試工作所需的測(cè)試框架,下面這篇文章主要給大家介紹了關(guān)于Python自動(dòng)化測(cè)試面試題的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • Python 獲取ftp服務(wù)器文件時(shí)間的方法

    Python 獲取ftp服務(wù)器文件時(shí)間的方法

    今天小編就為大家分享一篇Python 獲取ftp服務(wù)器文件時(shí)間的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python安裝pywifi全過程

    python安裝pywifi全過程

    這篇文章主要介紹了python安裝pywifi全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • python錯(cuò)誤:AttributeError: ''module'' object has no attribute ''setdefaultencoding''問題的解決方法

    python錯(cuò)誤:AttributeError: ''module'' object has no attribute

    這篇文章主要介紹了python錯(cuò)誤:AttributeError: 'module' object has no attribute 'setdefaultencoding'問題的解決方法,需要的朋友可以參考下
    2014-08-08
  • 10個(gè)Python常用的損失函數(shù)及代碼實(shí)現(xiàn)分享

    10個(gè)Python常用的損失函數(shù)及代碼實(shí)現(xiàn)分享

    損失函數(shù)是一種衡量模型與數(shù)據(jù)吻合程度的算法。損失函數(shù)測(cè)量實(shí)際測(cè)量值和預(yù)測(cè)值之間差距的一種方式。本文為大家總結(jié)了10個(gè)常用的損失函數(shù)及Python代碼實(shí)現(xiàn),需要的可以參考一下
    2022-09-09
  • python中List的sort方法指南

    python中List的sort方法指南

    我們需要對(duì)List進(jìn)行排序,Python提供了兩個(gè)方法:1.用List的成員函數(shù)sort進(jìn)行排序;2.用built-in函數(shù)sorted進(jìn)行排序,今天我們就來探討下這2個(gè)方法
    2014-09-09
  • Python圖像處理之模糊圖像判斷

    Python圖像處理之模糊圖像判斷

    這篇文章主要為大家詳細(xì)介紹了Python圖像處理中的模糊圖像判斷的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下
    2022-12-12
  • python實(shí)現(xiàn)sm2和sm4國(guó)密(國(guó)家商用密碼)算法的示例

    python實(shí)現(xiàn)sm2和sm4國(guó)密(國(guó)家商用密碼)算法的示例

    這篇文章主要介紹了python實(shí)現(xiàn)sm2和sm4國(guó)密(國(guó)家商用密碼)算法的示例,幫助大家使用python加密文件,感興趣的朋友可以了解下
    2020-09-09

最新評(píng)論