TL;DR:
The Goal - My goal with V2 is to get a for loop to execute the exact way a handful of if/elif statements do in my V1, but across multiple users that are stored in a JSON file.
The Problem (Presumably) - Splinter's is_text_present method doesn't want to accept the variables that I am loading from a JSON file.
Solutions Attempted: Outcome - Angleton's post: Didn't achieve the goal
Full Post:
I am trying to simplify a Python script by turning if/elif statements into a for loop.
The script's current version, V1, executes as expected and the code looks something like this:
V1 Final: Answers the Security Questions
if browser.is_text_present('Example_Question_1'):
browser.find_by_id('secretquestion0').first.fill('Example_Answer_1')
elif browser.is_text_present('Example_Question_2'):
browser.find_by_id('secretquestion0').first.fill('Example_Answer_2')
elif browser.is_text_present('Example_Question_3'):
browser.find_by_id('secretquestion0').first.fill('Example_Answer_3')
elif browser.is_text_present('Example_Question_4'):
browser.find_by_id('secretquestion0').first.fill('Example_Answer_4')
elif browser.is_text_present('Example_Question_5'):
browser.find_by_id('secretquestion0').first.fill('Example_Answer_5')
The script's second version, V2, pulls from a JSON file... the data in the JSON is organized like this:
V2: JSON File
{
"User": {
"username": "username",
"password": "password",
"Security_Questions": [
{
"question": "'Example_Question_1'",
"answer": "'Example_Answer_1'"
},
{
"question": "'Example_Question_2'",
"answer": "'Example_Answer_2'"
},
{
"question": "'Example_Question_3'",
"answer": "'Example_Answer_3'"
},
{
"question": "'Example_Question_4'",
"answer": "'Example_Answer_4'"
},
{
"question": "'Example_Question_5'",
"answer": "'Example_Answer_5'"
}
]
}
}
As I am building my V2, I believe I am running into a problem with Splinter's is_text_present method: It doesn't want to accept the question or answer variables that I am loading from the JSON File. I am not running into any errors... the script simply finishes with exit code 0.
I edited my JSON data to reflect the solution presented in Angleton's post, but had no luck with it.
Here is my first stab at the for loop that will replace the if/elif statements for the script's second version:
V2 Draft: Answers the Security Questions
for user in user_data:
.
.
.
for security_info in user_data[user]["Security_Questions"]:
question = security_info["question"]
answer = security_info["answer"]
if browser.is_text_present(question):
browser.find_by_id('secretquestion0').first.fill(answer)
My goal with V2 is to get this for loop to execute the exact way the V1 does, but across multiple users that I'll store in the JSON File.
Been at this for a while now and I am not sure what else I should try...
Can you help me solve this problem?
In the JSON, the questions contain an apostrophe (') at the end and start, so the browser tries to search for
'Example_Question_1','Example_Question_2'when the question, according to the V1, isExample_Question_1. You simply need to remove the apostrophes from the JSON file.