Capybara - Click element by class name

capybara within class
capybara click label
capybara click text
capybara click button class
capybara have_field
capybara find by name
capybara radio button
capybara table

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.

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')

Find in Capybara, Parent Note My Capybara note n350071 ・ Se Tagged with find by name. find('a', name: 'click here').click. find by element and class. Double Click the Element. If the driver dynamic pages (JS) and the element is currently non-interactable, this method will continuously retry the action until either the element becomes interactable or the maximum wait time expires.

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

Clicking elements that are not links or buttons. · Issue #1200 , I have a situation where I must click a span elements, which makes text() - this is what Capybara (XPath) uses internally for finding nodes by  Find element by text in Capybara. Think about a simple situation : You have an element but you’ve got not css selector , or any other type to identify that element , lucky you capybara user, you can find element by text in Capybara. Peace of cake , Capybara offers the :text option to go along with your selector:

I had a button that could not be found (Unable to find visible link or button nil with classes [close-modal]) using the methods above.

This worked for me: page.execute_script('$.find(".close-modal")[0].click()')

capybara cheat sheet · GitHub, click_link('Link Text'). click_button('Save'). click('Link Text') # Click either a link or a button. click('Button Value'). =Interacting with forms= fill_in('First Name', :with  Find a button on the page. This can be any <input> element of type submit, reset, image, button or it can be a <button> element. All buttons can be found by their id, name, test_id attribute, value, or title. <button> elements can also be found by their text content, and image <input> elements by their alt attribute.

click_on('.filter-case-studies')

You need the . selector for classes, and # for ids.

Capybara, Capybara - Click element by class name. capybara within capybara order capybara have_field capybara wait capybara click_button capybara click button class I have a situation where I must click a span elements, which makes parts of the DOM vissible (that I must verify). A javascript click eventhandler makes the part of the DOM vissble. But when using capybaras click_on it seems that it only

Thanks to Mr Schutte for the idea to use . selectors.

I had to use page.find(:class, '.filter-case-studies').click in the end. The absolute navbar got in the way of the button so I then had to include page.execute_script "window.scrollBy(0,500)" to complete the test.

Class: Capybara::Node::Element, #click(*modifier_keys, wait: nil, **offset) ⇒ Capybara::Node::Element. Click the Element. Double Click the Element. The tag name of the element. #text(type​  The getElementsByClassName() method returns a collection of all elements in the document with the specified class name, as an HTMLCollection object. The HTMLCollection object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.

Module: Capybara::Node::Finders, class (String, Array<String>, Regexp) —. Only find elements with matching class/​classes. Absence of a class can be checked by prefixing the class name with ! If you have required capybara/rails, Capybara.save_path will default to tmp/capybara. Matching. It is possible to customize how Capybara finds elements. At your disposal are two options, Capybara.exact and Capybara.match. Exactness. Capybara.exact and the exact option work together with the is expression inside the XPath gem.

Capybara Cheat Sheet, Triggers a click on a Capybara::Element. Works with JavaScript drivers. Trigger. find('input[name="post[title]  I'm having an issue with the Capybara check() method. I'm using Capybara 2.3.0 and Rails 4.1 , I don't know if it is related with the issues on this thread, but I don't seem able to let Capybara check multiple checkboxes using the check() method.

Capybara: Find an element that contains a string, E.g. a <div class="container"> that surrounds your entire layout will probably also contain that text (in a descendant) and Capybara will return it. See Find the  Correct method name is click_on, not click; default_selector in Capybara 2 is :css so examples in section scoping should be updated. Capybara 2.1 by default finds only visible elements so find_link('Hello').visible? doesn't really make sense; There's no such method as locate in Capybara

Comments
  • Ah I see what they mean by :css selectors now. That seems to have almost worked but now I get an error saying Element is not clickable at point (914.2666625976562, 16.133331298828125). Other element would receive the click: <div class="top-navbar ng-scope" ng-controller="menuCtrl"></div> (Selenium::WebDriver::Error::UnknownError) Since the navbar is absolute it must be getting in the way
  • click_on doesn't take a CSS selector, it takes the id, contained text, alt, title, etc - so this example should not work
  • It's true, I had to use page.find(:class, '.filter-case-studies').click for it to work. I'll post a proper solution
  • :class isn't a selector type provided by Capybara so the example you show wouldn't work unless you had registered a custom selector type.
  • Actually depending on the version of Capybara you are using it may either just ignore the :class parameter and default to :css, or it will output a warning about an 'Unknown selector type' and then default to :css . Also depending on the page, increasing the page size may remove the need for scrolling the page.