Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara - Click element by class name

For what seems to be a simple question I've been on this for a stupidly long time and can't seem to find anything on Google. I have this button I need to click which has no id but a class is included

<button class="filter-case-studies" onclick="initBootpag(filterForContentType('CASE STUDIES', searchHits))" type="button">
<b>CASE STUDIES</b>
(2)
</button>

I've tried using click_on which I now know is only for links and buttons so of course won't work. This is what I have so far:

When(/^I filter the results to only see case studies$/) do
  click_on('filter-case-studies')
end

I've also tried page.find('filter-case-studies').click, this too doesn't work.

page.find(:class, 'filter-case-studies').click defualts to :css so this also failed for me.

Is there no way to click an element by the class name in Capybara?

Thanks in advance for the help.

like image 828
King Avatar asked Apr 13 '17 15:04

King


2 Answers

The standard way of doing this in Capybara is

find('button.filter-case-studies').click

In relatively recent versions of Capybara you should also be able to do

click_on(class: 'filter-case-studies')
like image 85
Thomas Walpole Avatar answered Oct 26 '22 23:10

Thomas Walpole


find('.filter-case-studies').click as recommended here https://robots.thoughtbot.com/write-reliable-asynchronous-integration-tests-with-capybara#find-the-first-matching-element

like image 21
António Ferreira Avatar answered Oct 26 '22 23:10

António Ferreira