如何在 Capybara 中使用 fill_in 和 find (如果可能的话)

我想执行下面的操作,但是由于 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 一起使用的字段的最佳方法有什么想法吗?

61439 次浏览

If you have a reference to the element itself you'd use set instead of fill_in:

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

However for your specific example, fill_in should be able to find the element as you know it's ID:

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

It's worth noting that you can chain your finds.

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

Instead of a method, you can use brackets to return :name or :id, e.g. element = find(:css, "input[id$='donation_pledge_hundreds']") fill_in element[:name], :with => "10" The same approach can be used with select - select my_type, from: find('select[name$="[type]"]')[:name]

fill_in <$id>, :with => 'text you want to fill in'
element = find(:css, "input[id$='donation_pledge_hundreds']")
element.fill_in with: "10"