Capybara - click element by class name

For what seems like a simple question, I was stupid about it and couldn't find anything on Google for a long time. I have this button that I need to click, which has no id, but the class is included

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

      

I tried using click_on

which I now only know 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 also tried it page.find('filter-case-studies').click

, that doesn't work either.

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 class name in Capybara?

Thanks in advance for your help.

+7


source to share


4 answers


The standard way to do it in Capybara is

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

      



In relatively recent versions of Capybara, you can also do

click_on(class: 'filter-case-studies')

      

+12


source


click_on('.filter-case-studies')



You need a selector .

for classes, and #

for identifiers.

0


source


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

per recommendation https://robots.thoughtbot.com/write-reliable-asynchronous-integration-tests-with-capybara#find- the-first-matching -element

0


source


Thanks to Mr. Schutte for the idea of ​​using selectors .

.

I ended up having to use page.find(:class, '.filter-case-studies').click

. The absolute navigator got in the way of the button, so I had to turn it on page.execute_script "window.scrollBy(0,500)"

to complete the test.

-1


source







All Articles