user1735921
user1735921

Reputation: 1379

Rails capybaraAmbiguous match, found 50 elements matching css

I get this error:

Ambiguous match, found 50 elements matching css "input[value=\"delete\"]"

When I put the following code:

find('input[value="delete"]').first.click

On html file:

<div id="deletediv-38">
    <form class="button_to" onsubmit="return confirm('Are you sure you want to delete?')" method="post" action="/del/38">
        <input type="hidden" name="_method" value="delete" />
        <input type="submit" name="delete-38" value="delete" />
    </form>
</div>

<div id="deletediv-39">
    <form class="button_to" onsubmit="return confirm('Are you sure you want to delete?')" method="post" action="/del/39">
        <input type="hidden" name="_method" value="delete" />
        <input type="submit" name="delete-39" value="delete" />
    </form>
</div>
.
.
.

find('input[id="delete-38"]') doesn't work , it says element not found.

And I can't count as well. when I do

find('input[value="delete"]').count.should_be > 0

I again get the error:

Ambiguous match, found 50 elements matching css "input[value=\"delete\"]"

Upvotes: 0

Views: 279

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49880

To explain the errors you're having -

find will find one unique element, if there are none or more than one matching you will get an error. Your find('input[id="delete-38"]') fails because according to the html the only elements with ids are divs and have divs of the format deletediv-xx. If you were trying to find the inputs with the name of delete-38 you would do

find('input[name="delete-38"]')

As @dimakura stated to get the first element you use #first. If you want a count of the elements you need to use #all - like so

all('input[value="delete"]').count

If the page is dynamically changing and you want to know the count on the page once at least one has appeared then you could do

all('input[value=delete]', minimum: 1).count

which would use Capybaras waiting behavior for at least one to appear before returning (or it times out while waiting)

Upvotes: 3

dimakura
dimakura

Reputation: 7655

Try using this instead:

first('input[value="delete"]').click

Upvotes: 2

Related Questions