Я хочу автоматически ответить на вопрос безопасности на веб-странице. Вопрос выбирается случайным образом из 6 вопросов. У меня есть словарь, ключи и значения которого соответствуют вопросам и ответам соответственно. Я хочу использовать селен для поиска на веб-странице ключей, и если ключ найден, я хочу войти в поле ответа. Ниже моя неудачная попытка.
secuQA = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6}
# i want to loop through the keys in the secuQA and search the webpage for each of them
# i think I will have to use:
browser.find_element_by_name(keys)
# if question contains a key in the dict, fill answer field w/ corresp value
ansElem = browser.find_element_by_id("secu_answ")
ansElem.click()
ansElem.send_keys(secuQA[key])
Изменение: Ниже приведена часть части веб-страницы, которая, как мне показалось, имеет значение.
<form class="form_login" id="vbsecques" name="vbsecques" method="post" encType="multipart/form-data" action="fillquestions.aspx">
<label for="secu_ques" class="">Question 1 of 3:</label>
<div id="secu_ques" class="">
<div id="secu_ques" class="">In what city did you meet your spouse/significant other?</div>
</div>
<br />
<label for="secu_answ" class="">Answer</label>
<input type="password" id="secu_answ" name="secu_answ" class="form-control ehd_sec_ques" placeholder="Answer" autocomplete="off" required autofocus />
<input type="hidden" id="lid" name="lid" value="1" />
<input type="hidden" id="aflag" name="aflag" value="submit" />
<br />
<br />
<button class="btn btn-danger btn-block ehd_sec_ques" type="submit">Submit</button>
<br />
</form>
Сначала вы должны найти элемент вопроса.
q_element = driver.find_element_by_id("secu_ques")
question_strings = q_element.text.split(" ")#get a list of words from the question sentence
for key in secuQA:
if key in question_strings:
ans = secuQA[key]
break
Естественно, ключи, которые вы используете, должны быть уникальными для того, чтобы правильно определить соответствующий ответ. Позвольте мне знать, если это помогает.