How to select option in drop down using Capybara

后端 未结 9 1086
终归单人心
终归单人心 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:23

    Unfortunately, the most popular answer did not work for me entirely. I had to add .select_option to end of the statement

    select("option_name_here", from: "organizationSelect").select_option

    without the select_option, no select was being performed

    0 讨论(0)
  • 2020-12-24 00:24

    For some reason it didn't work for me. So I had to use something else.

    select "option_name_here", :from => "organizationSelect"
    

    worked for me.

    0 讨论(0)
  • 2020-12-24 00:24

    To add yet another answer to the pile (because apparently there's so many ways of doing it depending on your setup) - I did it by selecting the literal option element and clicking it

    find(".some-selector-for-dropdown option[value='1234']").select_option
    

    It's not very pretty, but it works :/

    0 讨论(0)
  • 2020-12-24 00:26

    Here's the most concise way I've found (using capybara 3.3.0 and chromium driver):

    all('#id-of-select option')[1].select_option
    

    will select the 2nd option. Increment the index as needed.

    0 讨论(0)
  • 2020-12-24 00:26

    It is not a direct answer, but you can (if your server permit):

    1) Create a model for your Organization; extra: It will be easier to populate your HTML.

    2) Create a factory (FactoryGirl) for your model;

    3) Create a list (create_list) with the factory;

    4) 'pick' (sample) a Organization from the list with:

    # Random select
    option = Organization.all.sample 
    
    # Select the FIRST(0) by id
    option = Organization.all[0] 
    
    # Select the SECOND(1) after some restriction
    option = Organization.where(some_attr: some_value)[2]
    option = Organization.where("some_attr OP some_value")[2] #OP is "=", "<", ">", so on... 
    
    0 讨论(0)
  • 2020-12-24 00:31

    In Capybara you can use only find with xpath

    find(:xpath, "//*[@id='organizationSelect']/option[2]").click
    

    and method click

    0 讨论(0)
提交回复
热议问题