2013-05-03 55 views
2

比方說,我有HTML這樣的:如何獲得圖像源值

<div class="out-radio"> 
    <div class="in-radio"> 
     <input type="radio" value = "12345"> 
     <label 
     <span   
      <div 
       <img src="http://s3.amazonaws.com/test001.jpg"> 
      </div> 
     </span 
    </label 
    </div> 
    <div class="in-radio"> 
     <input type="radio" value = "12346"> 
     <label 
     <span   
      <div 
       <img src="http://s3.amazonaws.com/test002.jpg"> 
      </div> 
     </span 
    </label 
    </div> 
    <div class="in-radio"> 
     <input type="radio" value = "12347"> 
     <label 
     <span   
      <div 
       <img src="http://s3.amazonaws.com/test003.jpg"> 
      </div> 
     </span 
    </label 
    </div> 
    <div class="in-radio"> 
     <input type="radio" value = "12348"> 
     <label 
     <span   
      <div 
       <img src="http://s3.amazonaws.com/test004.jpg"> 
      </div> 
     </span 
    </label 
    </div> 
    <div class="in-radio"> 
     <input type="radio" value = "12349"> 
     <label 
     <span   
      <div 
       <img src="http://s3.amazonaws.com/test005.jpg"> 
      </div> 
     </span 
    </label 
    </div> 
</div> 

如果我想顯示value所選單選按鈕,我會做這樣的:

browser.element(:xpath, '//*[@class = "in-radio"]').inputs.each {|input| puts value if input.checked?} 

如何顯示所選單選按鈕的src? 有什麼辦法,我可以把:

browser.element(:xpath, '//*[@class = "in-radio"]').(inputs,imgs).each {|input,img| ...} 
+0

的HTML是不是非常好,我來編輯它把它放在公衆。 – user2134050 2013-05-03 21:00:04

回答

5

如果選擇最後一個單選按鈕:

browser.radios.each {|radio| p radio.parent.img.src if radio.set?} 
# => "http://s3.amazonaws.com/test005.jpg" 
+0

我得到這個錯誤: Watir :: Exception :: UnknownObjectException:無法定位元素,使用{:tag_name =>「img」} 這可能是由於我的真實HTML代碼與我在這裏發佈的內容有點不同。我編輯了HTML ..抱歉,謝謝 – user2134050 2013-05-06 13:46:02