How to use fill_in with find in Capybara (if possible)

有些话、适合烂在心里 提交于 2019-12-28 08:06:13

问题


I'd like to do the following but can't due to the nature of fill_in expecting a locator as the first argument.

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

I've also tried doing

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

but there are no methods that return any data to identify the element to fill_in.

Any ideas of the best way of finding a field via a regex for use with fill_in?


回答1:


Going from memory so may not be 100% correct, but I think 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"



回答2:


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]




回答3:


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"



回答4:


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



回答5:


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


来源:https://stackoverflow.com/questions/8534365/how-to-use-fill-in-with-find-in-capybara-if-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!