查找標籤的使用水豚發現使用水豚
的文本標籤的文本背景:我有一個KBA網頁和一組問題和答案,答案都在使用無線電5個可能的答案形式鈕釦。所以我需要遍歷每個單選按鈕的每個標籤以匹配yaml文件中的有效答案。
HTML:
<div class="questions"></div>
<div class="answers"></div>
<p>
<label>
<input id="answers_question_0_1" type="radio" checked="checked" value="1" name="answers[question_0]"></input>
RADIO BUTTON TEXT 1
</label>
</p>
<p></p> #another radio button and label text 2
<p></p> #another radio button and label text 3
<p></p> #another radio button and label text 4
<p></p> #another radio button and label text 5
我的測試代碼:
def answer_questions
.
.
.
i=0
def answers
page.all('.answers')
end
#This is accessing the answer value from the selected correct question from the kba.yml file
valid_answers = this variable contains the valid answer to the question
#********THIS IS THE PROBLEM BLOCK*****************************
#Set the radio buttons if they match one of the answers
@correct_answer = answers[i].all(:radio_button).find do |radio|
valid_answers.include?(radio.parent.text)
end
#********THIS IS THE PROBLEM BLOCK*****************************
i +=1
unless @correct_answer
p "Unable to answer question: #{question_text}" and next
end
@correct_answer.select
end
問題就出在 「問題塊」 在上面的代碼片段指出。我無法弄清楚如何獲得每個單選按鈕綁定到一個label/p標籤的文本,而是返回所有單選按鈕的文本並將其與變量「valid_answers」進行覈對,並且始終失敗。
我基本上希望radio.button.text
等於「RADIO BUTTON TEXT 1」,因爲它是相應的單選按鈕。而是radio.button.text
回報:
單選按鈕TEXT 1個RADION按鈕文本2單選按鈕,文本3等
我猜它不應該是radio.parent.text
但別的東西,這我不知道。
湯姆,謝謝,你我走上了正確的道路,只是改變了你稍微說: 答案[I]。選擇(valid_answer [0 ]) – Farooq