How to read Config Files in Python using Selenium
On This Page What are Configuration Files in Python?April 24, 2026 · 10 min read · Tool Comparison
Managing configuration settings efficiently is significant when automating web covering with in Python. Config register let you store environment-specific parameters separately and load them dynamically within your Selenium scripts. What are Configuration Files in Python? Configuration file in Python are external files that store an application & # 8217; s settings, parameters, and preferences. These files let developers separate configuration particular from the main code. Types of Configuration Files in Python In this guide, learn more about several methods of say config files in Python and integrating them effectively with Selenium automation. Configuration files in Python are external file that store setting, parameters, and penchant for an application. These files let developer separate configuration details from the chief codification which makes the application more modular and simpler to contend. These file contain information like database credential, API key, file paths, and user-defined scene. The common formats or types for configuration files in Python are INI files, JSON file, YAML files, and ENV files. Read More: Configuration plays a crucial role in software ontogenesis and automation and offers a lot of benefits: Python supports multiple types of configuration files, each with its unique structure and use case. These formats assist shop and manage application background efficiently. This makes it easygoing to update form without alter code. This section continue some of the common character of configuration files, including INI, JSON, YAML, and ENV file. It stand forJavaScript Object Notation. It shop info in a key-value pair. Example: It stands forYAML Ain & # 8217; tMarkup Language. It also stores information in a key-value pair. The structure is slightly different from JSON. Example: It stand forINItialization file. It is really nonrational to parse and understand. Example: It stands for EXtensible Markup Language. It memory info in tags. Each tag name represents the belongings and the tag value match to the tag value. Example: All the above four configuration file convey the same information. The Enabled flag within that configuration is set to1. Read More: ConfigParser is a built-in Python module for managing configuration files in the INI format. It facilitates reading, write, and modifying configuration settings store in integrated key-value pairs under different sections. It is utilitarian for managing scope like database connections, API credentials, and user preferences in a integrated and readable format. Read More: There are multiple steps involved in managing configuration files expend ConfigParser, from creating and read files to update and write new configurations. Check out this step-by-step tutorial: Since ConfigParser is a built-in faculty in Python, no additional installment is need. You can simply spell it: Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script. A conformation file should be created with sections and key-value pairs: To update an be conformation setting: To delete a subdivision or a specific key: Read More: Here are some of the best practices to be followed for managing configuration files in Python: Read More: is the near popular programming speech in late clip. It has several libraries for catering use cases like, automation, etc. is a very popular python library commonly used for. It can be used to execute tasks like: There are various former applications of. In this article, we will focus on using Selenium for apython selenium config file. The test example will be stored in a config file, and we will extract the data from that config file in Python words and then execute the test case using Selenium. Prerequisites – Initial Requirements Step 1: Install Python Step 2: Install Selenium Once you have installed Python, you can open the bidding line (for windows) or the terminal (for Mac and Linux) and just use the pip command like below to install Selenium. Step 3: Download chromedriver Selenium needs a web driver to simulate a browser visit. Supported browser are: In this article, we will explore using Chrome and Firefox browsers. For that we necessitate to download the relevant chromedriver from the below location: And relevant geckodriver for Firefox from below position: You ask to store the chromedriver.exe and GeckoDriver in the folder where you are running your code. Read More: Here you have a very simple test case of opening Google and research for asearch_termand exhort enter. Thissearch_termwill be stored in a config filebrowserstack_config_v2.jsonThat file contains the below configuration Here, you will be guided on how to read this config file and perform the trial case. Step 1: Import necessary library Python Code: Step 2: Load the config file We will use the JSON library to load the json file hold configuration. Python Code: Output: Now, the config_data variable contains the configuration stored as a dictionary. Step 3: Load the configuration of the 1st browser and perform the exam. We will load the first configuration, and write our entire test case. We will besides publish whether the examination is successful or stillborn. Once, the test case runs, we will also close the driver Python Code: It will invoke a Selenium session i.e. it will open a Chrome browser and do the activity we cypher. Also, additionally, it will print whether the test is successful or unsuccessful. For our illustration, the test ran successfully, so we see the below output: Step 4: Putting it all together (we will save the entire python code as selenium_test.py) Step 5: Execute the command to run the test You can open the command line (for windows) or the terminal (for Mac and Linux) and execute the below command Output: Reading configuration file in Python using Selenium improves by get test code more maintainable, scalable, and easy to interpret. Using libraries like configparser for .ini file, json for JSON, and yaml for YAML allows testers to manage browser settings, credentials, and early parameter efficiently. This assist to define tests to run across multiple devices and browser. Also, it simplifies through consistent and reliable tests across multiple browser. Harnessing the total potentiality of goes a long way in enabling developers and QA to inspect, automate, and validate website functionality. Using tools like BrowserStack along with Selenium can help you run cross-browser and real-device tests seamlessly across 3500+ real-device-browser-OS combinations. On This Page # Ask-and-Contributeabout this topic with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.How to read Config Files in Python using Selenium
Overview
What are Configuration Files in Python?
Why Are Configuration Files Needed in Python?
Types of Configuration Files in Python
1. JSON Configuration Files
{'' Example '': {'' Language '': ['' Python ''], '' Modules '': ['' Selenium '',], '' Enabled '': 1}}2. YAML Configuration Files
Language: 'Python' Modules: - 'Selenium' Enabled: 1
3. INI Configuration Files
[instance] Language=Python Modules=Selenium enabled=1
4. XML Configuration Files
& lt; example & gt; & lt; language & gt; Python & lt; /language & gt; & lt; modules priority= '' 1 '' & gt; Selenium & lt; /modules & gt; & lt; enabled & gt; 1 & lt; /enabled & gt; & lt; /example & gt;
What is ConfigParser?
ConfigParser Benefits
Managing Python Configuration Files with ConfigParser
Step 1: Install and Import ConfigParser
import configparser
Step 2: Create a Configuration File
[Settings] username = admin password = secret url =
Step 3: Read Configuration Files
config = configparser.ConfigParser () config.read ('config.ini ') username = config ['Settings '] ['username '] password = config ['Settings '] ['password '] url = config ['Settings '] ['url '] print (f'Username: {username}, URL: {url} ')Step 4: Modify Configuration Files
config.set ('Settings ', 'username ', 'new_admin ') with open ('config.ini ', ' w ') as configfile: config.write (configfile)Step 5: Add New Sections and Keys
config.add_section ('Database ') config.set ('Database ', 'host ', 'localhost ') config.set ('Database ', 'port ', '5432 ') with open ('config.ini ', ' w ') as configfile: config.write (configfile)Step 6: Remove Sections or Keys
config.remove_section ('Database ') config.remove_option ('Settings ', 'password ') with unfastened ('config.ini ', ' w ') as configfile: config.write (configfile)Better Practices for Managing Configuration Files in Python
Understanding the persona of Python & amp; Selenium in Automation
pip install selenium
Problem Statement
{'' browsers '': [{'' browser '': `` chrome '', '' browser_version '': `` latest '', '' search_term '': `` browserstack ''}, {'' browser '': `` firefox '', '' browser_version '': `` up-to-the-minute '', '' search_term '': `` browserstack automate ''}]}from selenium import webdriver from selenium.webdriver.common.by importation By from selenium.webdriver.firefox.options import Options import json meaning warnings warnings.filterwarnings (`` ignore '')
with open ('browserstack_config_v2.json ') as json_file: config_data = json.load (json_file) mark (config_data){'browsers ': [{'browser ': 'chrome ', 'browser_version ': 'latest ', 'search_term ': 'browserstack '}, {'browser ': 'firefox ', 'browser_version ': 'latest ', 'search_term ': 'browserstack automate '}]}curr_config = config_data ['browsers '] [0] curr_browser = curr_config ['browser '] curr_search_term = curr_config ['search_term '] if (curr_browser=='chrome '): driver = webdriver.Chrome ('path to your chromedriver.exe ') elif (curr_browser=='firefox '): driver = webdriver.Firefox (executable_path= '' path to your geckodriver.exe ') try: mark (' 1. Opening Website ') url = 'http: //www.google.com' driver.get (url) mark (' 2. Performing test ') inputElement = driver.find_element (By.NAME, ' q ') inputElement.send_keys (curr_search_term) inputElement.submit () print (' 3. Test is Successful ') except Exception as e: print ('Test is Unsuccessful ') driver.close ()from selenium signification webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.common.by meaning By import json import monition warnings.filterwarnings (`` ignore '') with open ('browserstack_config_v2.json ') as json_file: config_data = json.load (json_file) for curr_config in config_data ['browsers ']: curr_browser = curr_config ['browser '] curr_search_term = curr_config ['search_term '] if (curr_browser=='chrome '): driver = webdriver.Chrome ('path to your chromedriver.exe ') elif (curr_browser=='firefox '): driver = webdriver.Firefox (executable_path= ’ path to your geckodriver.exe ') try: print (' 1. Opening Website ') url = 'http: //www.google.com' driver.get (url) print (' 2. Performing test ') inputElement = driver.find_element (By.NAME, ' q ') inputElement.send_keys (curr_search_term) inputElement.submit () print (' 3. Test is Successful ') except Exception as e: print ('Test is Unsuccessful ') driver.close ()python selenium_test.py
Conclusion
Utilitarian Resources for Selenium and Python
Related Guides
Automate This With SUSA
Test Your App Autonomously