TL;博士:
目标-我在V2中的目标是获得一个for
循环,以与V1中的少数if
/elif
语句完全相同的方式执行,但要跨存储在JSON文件中的多个用户执行。
问题(推测(-Splinter
的is_text_present
方法不想接受我从JSON文件加载的变量。
尝试的解决方案:结果-Angleton的帖子:没有达到的目标
完整帖子:
我试图通过将if
/elif
语句转换为for
循环来简化Python脚本。
脚本的当前版本V1按预期执行,代码如下所示:
V1期末考试:回答安全问题
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')
该脚本的第二个版本V2是从JSON文件中提取的。。。JSON中的数据组织如下:
V2:JSON文件
{
"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'"
}
]
}
}
在构建V2时,我相信Splinter
的is_text_present
方法遇到了问题:它不想接受我从JSON文件加载的question
或answer
变量。我没有遇到任何错误。。。脚本只是以退出代码0结束。
我编辑了我的JSON数据以反映Angleton帖子中提出的解决方案,但运气不佳
以下是我对for
循环的第一次尝试,它将替换脚本第二个版本的if
/elif
语句:
V2草案:回答安全问题
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)
我对V2的目标是让这个for
循环以与V1完全相同的方式执行,但要跨多个用户执行,我将存储在JSON文件中
我已经做了一段时间了,我不确定我还应该尝试什么。。。
你能帮我解决这个问题吗?
在JSON中,问题的末尾和开头都包含一个撇号('(,因此根据V1,当问题为Example_Question_1
时,浏览器会尝试搜索'Example_Question_1'
、'Example_Question_2'
。您只需要从JSON文件中删除撇号。