2016-03-09 60 views
0

查找標籤的使用水豚發現使用水豚

的文本標籤的文本

背景:我有一個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但別的東西,這我不知道。

回答

2

看起來你正在做這方面比它需要更爲複雜 - 你應該能夠做到

answers[i].choose("the text of the radio button you want to select") 

您可以捕捉異常,如果找不到該值和輸出你的警告那裏。

注意:水豚元素中的父代不是該元素的HTML父代 - 這是查找給定元素時調用查找器的元素 - 因此在您的情況下,它是.answers元素。如果你想訪問節點的實際HTML父元素,你可以撥打element.find(:xpath, '..')

+0

湯姆,謝謝,你我走上了正確的道路,只是改變了你稍微說: 答案[I]。選擇(valid_answer [0 ]) – Farooq