I would like to manage python selenium login ID and password in a separate file.

Asked 2 years ago, Updated 2 years ago, 119 views

Prerequisites/What you want to achieve

I would like to manage python selenium login ID and password in a separate file.
When I looked it up on the Internet, I heard that I would use configparser and ini, so I tried it.
I put the config.ini file on my desktop and went to load it, but it didn't work.

It doesn't have to be a configparser and ini file method, but what I want to do is to take my ID and PW out of the code and manage it by the user through text files.
Is there something wrong with the ini file?
If you understand, please let me know.

Problems/Error Messages you are experiencing

MissingSectionHeaderError: File contains no section headers.

Source Codes Affected

 [user]
name —nnnnnnn
password —ppppppppp

Tried

 from selenium import webdriver
import configparser

infile=configparser.ConfigParser()
infile.read (r'C:\Users\****\Desktop\config.ini', 'UTF-8')

driver = webdriver.Chrome("chromedriver.exe")

# driver = webdriver.Chrome() 
driver.get('http://www.****.biz/login/index.html') 
elem_username=driver.find_element_by_name('id') 
elem_username.send_keys(inifile.get('user', 'name')))
elem_password=driver.find_element_by_name('passwd')
elem_password.send_keys(inifile.get('user', 'password')))
driver.find_element_by_class_name('alpha').click()

driver.execute_script('close_popup();')

Supplementary information (for example, FW/Tool Version)

Python 3.7.3
Windows 7 jupyter notebook
chromedriver

python python3 selenium

2022-09-30 17:07

1 Answers

When editing the ini file in Notepad, it seems that the reason is that the BOM was attached.
Instead, creating a new file in Hidemaru solved the problem.

referenced sites:
"File contains no section headers.file:…" error and Trac project cannot be opened

This post is Posted as a community wiki based on @Tanichan's comment.


2022-09-30 17:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.