2012-02-08 74 views
0

所有!如何獲得水豚黃瓜元素集合

可能我有一個表

<tabel> 
    <tr><td><input type=text/></td><td><input type=text/></td></tr> 
    <tr><td><input type=text/></td><td><input type=text/></td></tr> 
    <tr><td><input type=text/></td><td><input type=text/></td></tr> 
    <tr><td><input type=text/></td><td><input type=text/></td></tr> 
</table> 

我想填補他們的週期,這樣

table.hashes.each do |hash| 
     within(:xpath,"//div[@id='cycle_form']/table/tr#{table.hashes.index(hash)}") do 
     fill_in("schedule_of_working_new_cycle_attributes__day", :with => hash['day']) 
     fill_in("schedule_of_working_new_cycle_attributes__hour", :with => hash['hour']) 
     fill_in("schedule_of_working_new_cycle_attributes__hour_night", :with => hash['hour_night']) 
     end 
    end 

我得到@找不到XPath的「黃瓜步驟定義// DIV [@ ID = 'cycle_form']/table/tr [0]「@

如何獲得水豚元素的集合?

+0

你有沒有嘗試在html中用id =「cycleform」命名你的div? – uday 2012-02-08 18:42:56

+1

在你的實際代碼中拼寫錯誤,或者當你在這裏發佈時,你是否拼錯了它?因爲這可能是你的問題。 – IanW 2012-02-08 18:44:56

+0

我不嘗試重命名我的div的id屬性,因爲我有另一個這樣的setip find(:xpath,「// div [@ id ='cycle_form']/table」)。should have_css(「tr td input [ @ type ='text']「)是通過。它的工作 – dilshod 2012-02-08 19:17:52

回答

0

這可能是與XML的問題,因此假定它是爲了看起來像這樣:

<div id="cycle_form"> 
    <table> 
     <tr><td><input type="text"/></td><td><input type="text"/></td></tr> 
     <tr><td><input type="text"/></td><td><input type="text"/></td></tr> 
     <tr><td><input type="text"/></td><td><input type="text"/></td></tr> 
     <tr><td><input type="text"/></td><td><input type="text"/></td></tr> 
    </table> 
</div> 

這將工作:

//div[@id='cycle_form']/table/tr[1] 

的另一件事是,XPath的索引開始於1不是0,所以你的原始查詢不會以任何方式工作。 仔細檢查輸入XML並更正迭代的起始值,它應該可以工作。

+0

我檢查並更正了所有。我的代碼在[這裏](http://copytaste.com/d831),但我stell得到@@無法找到xpath「// div [@ id ='cycleform']/table/tr [1]」(Capybara :: ElementNotFound) (eval):2:在'find' ./features/step_definitions/fill_schedule_of_working_steps.rb:72:in'block(2 levels)in '..... @@ – dilshod 2012-02-10 16:09:59