Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

This blog guides you on how to read a configuration file in Python using Selenium with examples and implementing the same on a cloud Selenium Grid.
Vinayak Sharma
December 22, 2025
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Python Tutorial.
Before starting with this tutorial on Python read config file let’s understand Selenium. Selenium is one of the best automation tools used for running automated tests through which you can perform relevant actions with the WebElements on the page. The major advantage of Selenium is that it supports a number of popular programming languages (e.g., Python, Java, JavaScript, C#, PHP, and Ruby).
When working with Selenium WebDriver, we need to follow the best practices to make the most out of the Selenium automation framework. One of them could be adding useful parameters like platform, browsers, browser versions, or device names in a manner that scaling up, organizing, and debugging the tests becomes easy. In addition, it should involve minimal changes in the test implementation! This is where configuration files come in handy, as configuration data can be added to those files with super ease. This, in turn, helps in improved maintenance of the test code.
In this Selenium Python tutorial, we deep dive into python read config file using Selenium. If you are preparing for an interview you can learn more through Selenium interview questions.
Configuration files usually contain information that is organized in key:value pairs, where value is recognized by key. Configuration files usually have extensions like .ini, .cnf, .conf, .cfg, and .cf. There are several types of modules and techniques to create and parse configuration files in Python.
Here are some of the commonly used configuration files in Python:
Though Python files are not used to store configuration information, the best part is that storing configuration in Python files is a simple task. The major advantage of this approach is the ease in the management of configuration variables. For example, to store these configurations, we can create a config.py file.
drivers_config = {
"URL": "localhost.com",
"FileName": "name.json",
}
# implementation comes here ...
You can use this config file in the actual implementation as shown below:
import config as cfg
driver = webdriver.Firefox()
driver.get(cfg.drivers_config["URL"])
As seen above, we simply imported the config file in the code before using the configuration parameters present in it. Now that we covered on python read config file, we now move to create JSON config files using the Python JSON module.
Note: If you are new to Python and want to get started step-by-step (with example), you can go through our Python Tutorial and become an expert in automation testing with Python.
CEO, Vercel
Discovered @TestMu AI yesterday. Best browser testing tool I've found for my use case. Great pricing model for the limited testing I do 👏
Deliver immersive digital experiences with Next-Generation Mobile Apps and Cross Browser Testing Cloud
This certification is for professionals looking to develop advanced, hands-on expertise in Selenium automation testing with Python and take their career to the next level.
Here’s a short glimpse of the Selenium Python 101 certification from TestMu AI:
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It provides information in a human-readable format, making it easy for machines to parse the information stored in the JSON file. Shown below is an example that demonstrates how to use Python’s JSON module for creating a config.json file.
{
"drivers_config":{
"URL":"localhost.com",
"FileName" : "file.json",
},
}
You can use this config file in the actual implementation as demonstrated below:
import json
# reading config file
file = open("config.json",)
data = json.load(file)
# using config file
print(data["drivers_config"]["URL"])
YAML (YAML Ain’t Markup Language) is a human-readable data serialization language. In Python, we use the YAML module to parse the YAML file.
drivers_config:
URL: localhost.com
FileName: file.json
other:
Variable: “data “
Here is how you can read the YAML config file in Python:
import yaml
file = open("config.yml", "r")
cfg = yaml.load(file)
print(cfg["drivers_config"]["URL"])
# this will print "localhost.com"
print(cfg["drivers_config"])
# this will print
# drivers_config
# {
# “URL” : “localhost.com”
# “NameFile”: “file.json”
# }
INI configuration file is a text-based structured syntax file that comprises key-value pairs.
[drivers_config]
URL=localhost.com
FileName=file.json
[other]
# other variables
To read this config file in Python, we will use the ConfigParser module. Run the below command on the terminal to install the ConfigParser module:
$ pip install configparser
Here is how you can parse the INI configuration file in Python:
import configparser
file = open("config.ini","r")
config = configparser.RawConfigParser(allow_no_value=True)
config.read_file(file)
print( config.get("drivers_config","URL") )
# output will be localhost.com
XML (Extensible Markup Language) lets you define rules for encoding documents that are easy to understand by humans and machines. The beautifulsoup module in Python is used for leveraging the advantages of XML configuration files.
Run the below command on the terminal to install the beautifulsoup module:
$ pip install bs4
<config>
<drivers_config>
<URL>localhost.com</URL>
<FileName>file.json</FileName>
</drivers_config>
</config>
To parse this file, we will use the bs4 or beautifulsoup module in the implementation. Once parsed, the Python file will look like this:
from bs4 import BeautifulSoup as bs
file = open('config.xml','r')
# parsing xml file
data = bs(file)
print(data.drivers_config.url.text)
Each parsing technique has its share of advantages & disadvantages. Hence, it is recommended to choose a technique that meets the project needs and requirements.
In the remaining part of Python read config file tutorial, we would use the INI configuration file since INI is the widely preferred configuration file format by Python developers.
So far in this tutorial on Python read config file, we have covered the essentials of python read config file. So, how can you leverage the benefits offered by configuration files in Python to make Selenium tests more readable and maintainable? Well, first, you need to move to a scalable and reliable cloud-based Selenium Grid so that you can run Selenium tests across different browsers and platform combinations.
TestMu AI is a cloud-based platform that enables you to perform live, interactive, and automated cross browser testing across 2000+ browsers and operating systems online. There are umpteen benefits of cloud testing, something that cannot be achieved by running Selenium tests on a local test infrastructure. Porting existing Python code to the TestMu AI Selenium Grid involves minimal changes, making it easy for QA engineers to leverage the platform in the shortest possible time.
Read – Detailed Selenium Python Tutorial
For demonstrating Python config file usage with an example, we would use the TestMu AI ToDo App on which the relevant tests are performed. The desired browser capabilities are generated using the TestMu AI capabilities generator.

Here are the set of operations performed on the TestMu AI Sample ToDo App:
check_box_one = driver.find_element_by_name("li1")
check_box_one.click()
check_box_two = driver.find_element_by_name("li2")
check_box_two.click()
# Enter item in textfield
textfield = driver.find_element_by_id("sampletodotext")
textfield.send_keys("text we need to add")
# Click on add button
add_button = driver.find_element_by_id("addbutton")
add_button.click()
# saving screenshot
driver.save_screenshot(filename.png)

[USER_INFO]
LT_USERNAME=YOUR_LT_USERNAME
LT_ACCESS_KEY=YOUR_LT_ACCESS_KEY
[SCREENSHOT_FILE_INFO]
FILENAME=filename.png
You could either pass this configuration file as a Command-Line argument or directly define it in the code.
We will see both methods for parsing the configuration file:
Though PyUnit is the default test automation framework in Python, QA engineers prefer PyTest for Selenium automation testing. To get started with PyTest, check out the below video from TestMu AI YouTube Channel.
In our case, we have named the Python configuration file as config.ini. This file will be parsed to read the relevant configuration information for usage in the code.
Here is how we open and read contents from the config file:
## getting config
with open("config.ini","r") as file:
config = configparser.RawConfigParser(allow_no_value=True)
config.read_file(file)
Here is the entire implementation where we read the LT_USERNAME and LT_ACCESS_KEY from the configuration file. The read information is further used in the code for accessing the TestMu AI Grid. We have the PyUnit framework for the entire demonstration. Please refer to our PyUnit Framework Tutorial to get more information on the said framework.
import configparser
import unittest
from selenium import webdriver
# getting config
with open("config.ini", "r") as file:
config = configparser.RawConfigParser(allow_no_value=True)
config.read_file(file)
username = config.get("USER_INFO", "LT_USERNAME")
access_key = config.get("USER_INFO", "LT_ACCESS_KEY")
class FirstSampleTest(unittest.TestCase):
# Generate capabilities here https://www.lambdatest.com/capabilities-generator/
# setUp runs before each test case and
def setUp(self):
desired_caps = {
"build": config.get("CAPABILITIES", "build"),
"name": config.get("CAPABILITIES", "name"),
"platform": config.get("CAPABILITIES", "platform"),
"browserName": config.get("CAPABILITIES", "browserName"),
"version": config.get("CAPABILITIES", "version"),
"selenium_version": config.get("CAPABILITIES", "selenium_version"),
"visual": config.get("CAPABILITIES", "visual"),
"geoLocation": config.get("CAPABILITIES", "geoLocation"),
"chrome.driver": config.get("CAPABILITIES", "chrome_driver"),
}
"""
Setup remote driver
-------
username and access_key can be found on lt platform
"""
self.driver = webdriver.Remote(
command_executor="https://{}:{}@hub.lambdatest.com/wd/hub".format(
username, access_key
),
desired_capabilities=desired_caps,
)
# tearDown runs after each test case
def tearDown(self):
self.driver.quit()
# """ You can write the test cases here """
def test_unit_user_should_able_to_add_item(self):
# try:
driver = self.driver
# Url
driver.get("https://lambdatest.github.io/sample-todo-app/")
# Click on checkbox
check_box_one = driver.find_element_by_name("li1")
check_box_one.click()
# Click on checkbox
check_box_two = driver.find_element_by_name("li2")
check_box_two.click()
# Enter item in textfield
textfield = driver.find_element_by_id("sampletodotext")
textfield.send_keys(config.get("ITEM_TO_ADD", "ITEM_VALUE"))
# Click on add button
add_button = driver.find_element_by_id("addbutton")
add_button.click()
# navigation to list to find the username to added item
custom_xpath = (
"//*[contains(text()," + config.get("ITEM_TO_ADD", "ITEM_VALUE") + ")]"
)
added_item = driver.find_element_by_xpath(custom_xpath).text
# saving screenshot
driver.save_screenshot(config.get("SCREENSHOT_FILE_INFO", "FILENAME"))
if __name__ == "__main__":
unittest.main()
For accepting the command-line argument(s), we will use the sys module in Python to pass the configuration name.
Here are some of the Python modules that can be used for passing command-line argument(s):
For demonstration, we have used the sys module to read the command-line argument(s):
import configparser
import unittest
import sys
from selenium import webdriver
class sampleTest(unittest.TestCase):
# default file name in case no arg passed
config_file = "config.ini"
def setUp(self):
self.config = configparser.RawConfigParser(allow_no_value=True)
self.config.read(self.config_file)
username = self.config.get("USER_INFO", "LT_USERNAME")
access_key = self.config.get("USER_INFO", "LT_ACCESS_KEY")
## get CAPABILITIES from https://www.lambdatest.com/capabilities-generator/
desired_caps = {
"build": self.config.get("CAPABILITIES", "build"),
"name": self.config.get("CAPABILITIES", "name"),
"platform": self.config.get("CAPABILITIES", "platform"),
"browserName": self.config.get("CAPABILITIES", "browserName"),
"version": self.config.get("CAPABILITIES", "version"),
"selenium_version": self.config.get("CAPABILITIES", "selenium_version"),
"visual": self.config.get("CAPABILITIES", "visual"),
"geoLocation": self.config.get("CAPABILITIES", "geoLocation"),
"chrome.driver": self.config.get("CAPABILITIES", "chrome_driver"),
}
"""
Setup remote driver
-------
username and access_key can be found on lt platform
"""
self.driver = webdriver.Remote(
command_executor="https://{}:{}@hub.lambdatest.com/wd/hub".format(
username, access_key
),
desired_capabilities=desired_caps,
)
# tearDown runs after each test case
def tearDown(self):
self.driver.quit()
# """ You can write the test cases here """
def test_unit_user_should_able_to_add_item(self):
# try:
driver = self.driver
# Url
driver.get("https://lambdatest.github.io/sample-todo-app/")
# Click on checkbox
check_box_one = driver.find_element_by_name("li1")
check_box_one.click()
# Click on checkbox
check_box_two = driver.find_element_by_name("li2")
check_box_two.click()
# Enter item in textfield
textfield = driver.find_element_by_id("sampletodotext")
textfield.send_keys(self.config.get("ITEM_TO_ADD", "ITEM_VALUE"))
# Click on add button
add_button = driver.find_element_by_id("addbutton")
add_button.click()
# navigation to list to find added item
custom_xpath = (
"//*[contains(text()," + self.config.get("ITEM_TO_ADD", "ITEM_VALUE") + ")]"
)
added_item = driver.find_element_by_xpath(custom_xpath).text
# saving screenshot
driver.save_screenshot(self.config.get("SCREENSHOT_FILE_INFO", "FILENAME"))
if __name__ == "__main__":
if len(sys.argv) > 1:
sampleTest.config_file = sys.argv.pop()
unittest.main()
Trigger the following command on the terminal to read the command-line arguments:
$ python -m single_test.py config.ini
Visit the Automation Dashboard on TestMu AI to get further information about the execution of tests on the cloud grid:

Do you know how to upload and download a file in Selenium? Watch this video to learn how to upload and download files in Selenium WebDriver using different techniques.
In this Selenium Java tutorial, we have learnt about python read config file using the common configuration file formats like .cnf, .conf, .cfg, .cf, and .ini. We have also seen how multiple configuration files are read together, and their results can be merged into a single configuration using ConfigParser.
We then performed Selenium automation testing to test the “add” functionality in the to-do list app and take a screenshot to verify our results using a cloud Selenium Grid.
Did you find this page helpful?
More Related Hubs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance