selenium+python自動化,資料驅動執行個體

來源:互聯網
上載者:User

標籤:web   value   roo   bsp   sim   csv   min   use   lines   

1.讀取txt

read() 讀取整個檔案。

readline() 讀取一行資料。

readlines() 讀取所有行的資料。

例:#coding=utf-8from selenium import webdriver

user_file = open(‘user_info.txt‘,‘r‘)

values = user_file.readlines()

user_file.close()

for serch in values:

  username = serch.split(‘,‘)[0]

  print username

    password = serch.split(‘,‘)[1]    print password

2.讀取csv

#coding=utf-8
import csv

#讀取本地 CSV 檔案
my_file=‘info.csv‘date=csv.reader(file(my_file,‘rb‘))

#迴圈輸出每一行資訊

for user in date:

  print user

3.讀取xml

XML 即可延伸標記語言 (XML),它可以用來標記資料、定義資料類型,是一種允許使用者對自己的標記語言進 行定義的源語言。

xml 有如下特徵: 首先,它是有標籤對組成:<aa></aa> 標籤可以有屬性: <aa id=’123’></aa> 標籤對可以嵌入資料: <aa>abc</aa>

例:#coding=utf-8
import xml.dom.minidom

#開啟 xml 文檔
dom = xml.dom.minidom.parse(‘info.xml‘)

#得到文件項目對象
root = dom.documentElementprint root.nodeName
print root.nodeValue
print root.nodeType
print root.ELEMENT_NODE

 

mxl.dom.minidom
mxl.dom.minidom 模組被用來處理 xml 檔案,所以要先引入。

parse()
xml.dom.minidom.parse() 用於開啟一個 xml 檔案,並將這個檔案對象 dom 變數。

documentElement
documentElement 用於得到 dom 對象的文件項目,並把獲得的對象給 root

每一個結點都有它的 nodeName,nodeValue,nodeType 屬性。nodeName 為結點名字。
nodeValue 是結點的值,只對文本結點有效。
nodeType 是結點的類型。

 

getElementByTagName()可以通過標籤名擷取某個標籤。它所擷取的對象是以數組形式存放。如 “caption”和“item”標籤在 info.xml 檔案中有多個,那麼可以指定數組的下標在擷取某個標籤。

root.getElementsByTagName(‘caption‘) 獲得的是標籤為 caption 一組標籤; tagname[0] 表示一組標籤中的第一個。
tagname[2] 表示一組標籤中的第三個。

getAttribute() getAttribute()方法可以獲得元素的屬性所對應的值。

 

selenium+python自動化,資料驅動執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.