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.
source to share
find('.filter-case-studies').click
per recommendation https://robots.thoughtbot.com/write-reliable-asynchronous-integration-tests-with-capybara#find- the-first-matching -element
source to share
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.
source to share