Python+selenium之讀取配置文件內容


Python+selenium之讀取配置文件內容

  1. Python支持很多配置文件的讀寫,此例子中介紹一種配置文件的讀取數據,叫ini文件,python中有一個類ConfigParser支持讀ini文件。
  2. 將ini文件命名為config.ini,代碼如下所示

 

#this is config file,only store browser type and server URL

 

[browserType]

#browserName=Firefox

browserName=Chrome

#browserName=IE

 

[testServer]

URL=https://www.baidu.com/

  3.然后新建一個測試python文件,命名為testconfig.py,測試腳本如下所示

 

#coding:utf-8

import ConfigParser

import os

 

class TestReadConfig(object):

 

    def get_value(self):

        root_dir=os.path.dirname(os.path.abspath('.'))#表示獲取當前文件夾的所在的目錄

        print root_dir

 

        config=ConfigParser.ConfigParser()

        file_path=os.path.dirname(os.path.abspath('.'))+'/pro1/config/config.ini'

        config.read(file_path)

 

        browser=config.get("browserType","browserName")

        url=config.get("testServer","URL")

        return (browser,url)

 

trcf=TestReadConfig()

print trcf.get_value()

注意:config.read(file_path),file_path這里的路徑需寫對,若不太清楚路徑,可以通過pycharm中的Debug來設置斷點,進行查看。【F8】運行到下一行

  4.測試結果如下圖所示:

詳情參考:http://blog.csdn.net/u011541946/article/details/70174276


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM