Neil
Neil

Reputation: 5178

Capybara find anchor element by data attribute

With capybara I am trying to find an anchor element by its data attribute so that I can click it. This anchor element is dynamic so the easiest way for me to grab it is by its data attribute.

I did look at the Capybara docs on the find method, and this similar question's suggestion was not working out for me. I am either making a syntax error or I am missing something.

Within my anchor tag here is my data attribute:

data-delete-association-field-name=“item[item_orders_attributes][0][_destroy]"

So with Capybara I am ultimately trying to find the anchor tag with that data attribute and then click it. Here is my current implementation which is not working:

find(‘a[data-delete-association-field-name=item[item_orders_attributes][0][_destroy]]’).click

Upvotes: 2

Views: 1528

Answers (1)

Neil
Neil

Reputation: 5178

Figured it out. I needed to specify the css selector for the data attribute.

This question shows the syntax for a css selector by data attribute. I then just had to apply that to the capybara find method like so:

find(‘a[data-delete-association-field-name="item[item_orders_attributes][0][_destroy]"]’).click

Upvotes: 3

Related Questions