I need to login to a webpage using selenium webdriver to download some PDFs, for this I need to use 2 sets of credentials that i need to pass as plaintext.
What would be the best way to do this without hardcoding the passwords?
i was hardcoding everything like this:
if __name__ == '__main__':
ooffice=[SECRET EXPUNGED]
luser=[SECRET EXPUNGED]
lpassword=[SECRET EXPUNGED]
muser=[SECRET EXPUNGED]
mpassword=[SECRET EXPUNGED]
logname="log.txt"
today=datetime.datetime.now()
with open(logname, 'a+') as f:
f.write("{}: program started \n".format(datetime.datetime.now()))
for i in range(3):
try:
with open(logname, 'a') as f:
f.write("{}: downloading first document, try:{} \n".format(datetime.datetime.now(),i+1))
simpledownload(ooffice,luser,lpassword)
break
except Exception as Argument:
with open(logname, 'a') as f:
f.write(str(Argument))
f.write("\n")
for i in range(3):
try:
simpledownload(ooffice,muser,mpassword)
with open(logname, 'a') as f:
f.write("{}: downloading second document, try:{} \n".format(datetime.datetime.now(),i+1))
break
except Exception as Argument:
with open('output.txt', 'a') as f:
f.write(str(Argument))
f.write("\n")
you can use a separate configuration file or environment variables to store them. at first, you can create a file named 'config.ini' in the same directory as your script with the following contents.
[credentials]
luser = username1
lpassword = password1
muser = username2
mpassword = password2
then in your script, use the configparser module to read the credentials from the configuration file.
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
luser = config['credentials']['luser']
lpassword = config['credentials']['lpassword']
muser = config['credentials']['muser']
mpassword = config['credentials']['mpassword']
now you can use the variables luser, lpassword, muser, and mpassword in your login code instead of hardcoding the values.