1
votes

I've been asked by a colleague to help with his problem but I too, am becoming stuck on it.

He wants to find a clickable element by the text on it from a list he's got.

The list contains:

list = [ 'cat : B',
         'cat : B1',
         'cat : BE'
       ]

When he iterates over the list, the first iteration clicks on cat : B as expected. The next iteration also clicks on cat : B instead of B1.

the code used is:

list.each do |cat_item|
find('li.category', text: cat_item, :match => :prefer_exact).click

It's looks as though Capybara is only matching as far as the first B and thinking it's found the element, instead of matching to B1 or BE.

Is there a way around this? I've tried several ways

1

1 Answers

0
votes

The :prefer_exact match option has no effect on the :text option ( neither does exact: true - they only affects selectors that use the XPath.is method - selectors like :field, :label, :button, etc which take a string to potentially match against label text ). To do exact matching with the :text option you need to pass a Regexp. That said, it shouldn't be matching just B when you pass B1, have you printed out the text of the found elements to confirm which elements it's actually finding, and that it's not an issue with your click handler?