Capybara can find but not fill_in

后端 未结 3 854
梦如初夏
梦如初夏 2021-02-13 16:50

I am having some very strange behaviour with Capybara. It stubbornly refuses to fill in the fields of my login form.

相关标签:
3条回答
  • 2021-02-13 17:46

    The locator for find and fill_in are different:

    • find - When the first parameter is not a symbol, it is assumed to be the Capybara.default_selector - ie a css-selector or xpath.
    • fill_in - The first parameter is the field's name, id or label text.

    The string "#user_email" represents a css-selector. This is why it works in find but not fill_in.

    For fill_in to work, you need to just pass in the id value - ie just "user_email".

    within("#login_form"){ fill_in("user_email", with: "foo@example.com")}  
    
    0 讨论(0)
  • 2021-02-13 17:51

    you can do find("#user_email").set "foo@example.com". See answer https://stackoverflow.com/a/8544650/3163914

    0 讨论(0)
  • 2021-02-13 17:52

    if it is an autocomplete field, you can use this:

      def fill_in_autocomplete(id, value)
        page.execute_script("document.getElementById('#{id}').setAttribute('value', '#{value}');")
      end
    
    
    0 讨论(0)
提交回复
热议问题