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

前端 未结 5 568
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 18:02

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_hun         


        
相关标签:
5条回答
  • 2020-12-07 18:38

    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"
    
    0 讨论(0)
  • 2020-12-07 18:48
    fill_in <$id>, :with => 'text you want to fill in'
    
    0 讨论(0)
  • 2020-12-07 18:50
    element = find(:css, "input[id$='donation_pledge_hundreds']")   
    element.fill_in with: "10"
    
    0 讨论(0)
  • 2020-12-07 18:55

    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]

    0 讨论(0)
  • 2020-12-07 18:58
    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"
    
    0 讨论(0)
提交回复
热议问题