Home > Software engineering >  Unable to select option using capybara
Unable to select option using capybara

Time:07-23

I am unable to select an option from an html element using capybara

   <select multiple="multiple" name="profile_invitation[permissions][]" id="profile_invitation_permissions">
    <option value="manage_permissions">Manage Permissions</option>
    </select>

I've tried

#within 'select#profile_invitation_permissions' do
    #find("option[@value='manage_permissions']").click
  #end

which returns

Unable to find css "option[@value='manage_permissions']" within #<Capybara::Node::Element tag="select" path="/html/body/div/div/main/div/div[2]/div[3]/div/div[2]/form/select"> (Capybara::ElementNotFound)

and I've tried

select "Manage Permissions", :from => "select#profile_invitation_permissions"

which returns

 Unable to find select box "select#profile_invitation_permissions" that is not disabled and Unable to find input box with datalist completion "select#profile_invitation_permissions" that is not disabled (Capybara::ElementNotFound)

CodePudding user response:

You have two "Manage Permissions"/value='manage_permissions' option tags, and why do you have @value= instead of just value= in your attribute selector?

CodePudding user response:

You're making it more complicated than it is. From the docs - https://rubydoc.info/github/teamcapybara/capybara/Capybara/Node/Actions#select-instance_method - we can see that select takes the text of the option to be selected, and optionally a from setting to specify either the id, name, or label of the select box. Therefore if there is only one "Manage Permissions" option on the page you can just do

select 'Manage Permissions'

If there is more than one matching option on the page then you would narrow it down using the from option

select 'Manage Permissions', from: 'profile_invitation_permissions'
  • Related