How to select option in drop down using Capybara

后端 未结 9 1087
终归单人心
终归单人心 2020-12-23 23:47

I\'m trying to select an item from a drop down menu using Capybara (2.1.0).

I want to select by number (meaning select the second, third, etc option).

I\'ve

相关标签:
9条回答
  • 2020-12-24 00:33

    none of the answers worked for me in 2017 with capybara 2.7. I got "ArgumentError: wrong number of arguments (given 2, expected 0)"

    But this did:

    find('#organizationSelect').all(:css, 'option').find { |o| o.value == 'option_name_here' }.select_option
    
    0 讨论(0)
  • 2020-12-24 00:38

    another option is to add a method like this

      def select_option(css_selector, value)
        find(:css, css_selector).find(:option, value).select_option
      end
    
    0 讨论(0)
  • 2020-12-24 00:39

    If you take a look at the source of the select method, you can see that what it does when you pass a from key is essentially:

    find(:select, from, options).find(:option, value, options).select_option
    

    In other words, it finds the <select> you're interested in, then finds the <option> within that, then calls select_option on the <option> node.

    You've already pretty much done the first two things, I'd just rearrange them. Then you can tack the select_option method on the end:

    find('#organizationSelect').find(:xpath, 'option[2]').select_option
    
    0 讨论(0)
提交回复
热议问题