Search code examples
cssrubyseleniumcapybara

Capybara, Ambiguous match, found 2 elements - how to select an unknown, unique selector


I am writing a UI test for a page that has two buttons that I can successfully target, but I cannot find the unique selector that is hidden to describe each button. I have my test spec file that is validating response, and referencing a class method file where I am defining where the element is.

I'm first asking if there is a way to target the " Send Club Assignment " string, because it's a unique thing, but it seems out of reach since it's not a text: element, and/or not in the element I'm actually matching, but nested in the <span class=' ... yadda yadda ... /span> under it. Or I want to know how to format either the test file, or class method file to be able to select the 2nd of the elements I am currently selecting

Here is a snippet of the test file

it "should log in, and add a club assignment to a test user" do
  profile_memberships_tab.load
  league_login
  expect(profile_memberships_tab.actionable_redirect_header).to be_visible
  click_on(profile_memberships_tab.send_club_assignment_button).click
  expect(page).to have_content("Send Direct Club Assignment Request")
end

Here is the class method file

class ProfileMembershipsTab < SitePrism::Page
  set_url "[webpage url]"
  element :send_club_assignment_button, "button", class: "se-button--medium" # this fails
  element :actionable_redirect_header, "h1", text: "Actionable Redirect" # this works
end

Here is a snippet of what the html looks like that I'm targeting enter image description here

I have tried

  • putting class method file to

    send_club_assignment_button, "button", class: "se-button--medium", text: "Send Club Assignment"

Which returns :

Capybara::ElementNotFound:
       Unable to find visible css "button" with text "Send Club Assignment" with classes [se-button--medium]
  • I tried wrapping putting the class file in multiple lines so I could make it the last one

    element :send_club_assignment_buttons, "button", class: "se-button--medium" element :send_club_assignment_button, send_club_assignment_buttons.last

  • I tried changing the test file to

click_on(profile_memberships_tab.last(send_club_assignment_button)).click

  • And other variations on the above - all of those return this:

    Capybara::Ambiguous: Ambiguous match, found 2 elements matching visible css "button" with classes [se-button--medium]


Solution

  • You can use the "find" helper from Capybara, example:

    find('se-button--medium').click
    

    With this helper, you can find elements based on css classes and then, click them

    See more in: https://rubydoc.info/github/teamcapybara/capybara/master#finding