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

ant*_*nts 89 find capybara

我想做以下但不能由于fill_in的性质期望定位器作为第一个参数.

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

我也尝试过

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

但是没有方法可以返回任何数据来识别fill_in元素.

有关通过正则表达式查找字段以与fill_in一起使用的最佳方法的任何想法吗?

Jon*_*n M 155

从内存开始可能不是100%正确,但我认为如果你有一个元素本身的引用,你将使用set而不是fill_in:

find(:css, "input[id$='donation_pledge_hundreds']").set("10")
Run Code Online (Sandbox Code Playgroud)

但是对于您的具体示例,fill_in应该能够找到您知道它的ID的元素:

fill_in 'donation_pledge_hundreds', with: "10"
Run Code Online (Sandbox Code Playgroud)

  • 非常混乱的界面...元素#fill_in for element应该只填写... (18认同)
  • 小心使用'set'而不是其他内置方法,因为它在更改值后不会触发事件. (8认同)
  • 您可以使用`find(:css, "...").set("10").send_keys(:return)` 之后按回车键。它触发相关事件。我还没有测试过,但你也可以`find(:css, "...").set("10").trigger(:blur)`,如果你喜欢的话。 (2认同)

小智 8

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


bhf*_*lor 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]