2011-12-16 61 views
80

我想要做以下事情,但不能因爲fill_in的性質而期待locator作爲第一個參數。如何在水豚中使用fill_in查找(如果可能的話)

find(:css, "input[id$='donation_pledge_hundreds']").fill_in :with => "10" 

我也試着做

element = find(:css, "input[id$='donation_pledge_hundreds']") 
fill_in element.<method> , :with => "10" 

但有沒有方法返回的任何數據來識別元素FILL_IN。

任何想法通過正則表達式找到一個字段的最佳方式與fill_in一起使用?

回答

136

從內存中去,從而可能不是100%正確的,但我認爲,如果你有元素本身的引用,你會使用set代替fill_in

find(:css, "input[id$='donation_pledge_hundreds']").set("10") 

但是你具體的例子,fill_in應該能夠找到元素,你知道它的ID:

fill_in 'donation_pledge_hundreds', with: "10" 
+0

好男人,這就是一個。該特定字段用於不同的上下文(已驗證/未驗證),因此需要不同的字段ID。 donation_pledge_hundreds是字段ID的公用部分,因此比較字段名稱的末尾$ = – ants 2011-12-17 12:33:33

3
find("input[id$='donation_pledge_hundreds']").set "10" 

值得一提的是,你可以鏈接你的發現。

@modal = find(".modal") 
@modal.find('input[name=foo]').set "bar" 
5

相反的方法,你可以用括號返回:name:id,例如 element = find(:css, "input[id$='donation_pledge_hundreds']") fill_in element[:name], :with => "10" 同樣的方法可以用select使用 - select my_type, from: find('select[name$="[type]"]')[:name]

0
fill_in <$id>, :with => 'text you want to fill in'