toddyznz toddyznz - 5 months ago 8x
Ruby Question

Capybara 'Ambiguous matching link or button nil'

inspecting web element

code to find element

def choose_ifp_term(ifp_term)
if ifp_term == "Pay outright"
Log.debug"Pay outright!"
elsif ifp_term == "Interest Free 12 month"
within page.first("div.tripleColumn") do
Log.debug"Not Found!"

I've been trying to click an anchor tag that is nested in a div element, and Im trying to click 'x' element based on the arguement that I'm passing through.

I get an ambiguous match when trying to click the element based on the arguement.

Ambiguous match, found 3 elements matching link or button nil (Capybara::Ambiguous)

The two images attached are the code and the element on the page. Please Help

SOLVED : with the help of @Tom


As I could not find element based on regex argument passed, so had to click based on position!


#all returns an array like collection of elements, so calling text on it isn't valid and I'm not sure why that isn't raising an error for you. Additionally, #all has a number of drawbacks (not waiting, not able to reload the found elements if they change, etc) and should be used only when absolutely necessary. In your case where you appear to be looking for a link that matches a regex you can replace your use of #all with #find

find('a', text: /#{ifp_term}/)

so if using older Capybara all together it would be

page.find("div.device-payment-option-container").click_link_or_button(find('a', text: /#{ifp_term}/).text)

However if you're using Capybara 2.7 you should be able to do

page.find("div.device-payment-option-container").click_link_or_button(nil, text: /#{ifp_term}/)

and if using Capybara master which will become 2.8 you should be able to just do

page.find("div.device-payment-option-container").click_link_or_button(text: /#{ifp_term}/)