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

原文地址:https://www.cnblogs.com/Rita-LJ/p/8079115.html