Capybara - check for the existence of a data attribute on a link
10,574
Solution 1
I have the same query, but the quotes are different.
find("a[data-method='delete']").click
and works for me.
Solution 2
Here are some different ways:
expect(page).to have_selector("a[href='#'][data-content='This is a discrete bar chart.']")
page.has_selector?("a[href='#'][data-content='This is a discrete bar chart.']")
page.find("a[href='#'][data-content='This is a discrete bar chart.']") # returns the node found
If you don't have access to page but have access to rendered, then
expect(Capybara.string(rendered)).to have_selector("a[href='#'][data-content='This is a discrete bar chart.']")
Solution 3
It could be an issue of Capybara finding too many results. Maybe change 'find' to 'all' and see if you get an array of results that you could select from. Best of luck.
Author by
bumpkin
Updated on June 15, 2022Comments
-
bumpkin almost 2 years
I have a link with a specific attribute on my page. Using Rspec + Capybara how can I check for the existence of this link?
<a href="#" id="text" data-content="This is a discrete bar chart.">Foo</a>
Does not work:
page.find(:css, 'a[data-content="This is a discrete bar chart."]')
Works:
page.find(:css, 'a#test')
-
Tim Moore over 10 yearsThat looks like it should work. Can you please post the full error message that you see?
-
method over 7 yearsSomething to be aware of with this kind of selector is that Capybara selectors match on attributes, not properties, meaning that if you add or change a data value after the page loads you won't be able to find the element using the selector for the data attribute.
-
-
Jon Kern almost 7 yearsA gentle note: This probably should have been a comment to the original question to get clarification from the OP. As this really is not an answer/solution.