Capybara - Click element by class name
capybara click label
capybara click text
capybara click button class
capybara find by name
capybara radio button
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
In relatively recent versions of Capybara you should also be able to do
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:
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.
You need the
. selector for classes, and
# for ids.
Thanks to Mr Schutte for the idea to use
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: 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
- 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_ondoesn'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').clickfor it to work. I'll post a proper solution
:classisn'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
:classparameter 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.