Capybara won't find a button by its "name" attribute Capybara won't find a button by its "name" attribute ruby ruby

Capybara won't find a button by its "name" attribute


It turns out the docs are misleading for both calls, as neither look at the attributes listed. It's also clearly very confusing what exactly a "button" means, since a couple of people herein seemed to think it literally only meant an HTML button element but that's not the case.

If you view the source for the documentation of, say, click_button:

https://github.com/jnicklas/capybara/blob/a94dfbc4d07dcfe53bbea334f7f47f584737a0c0/lib/capybara/node/actions.rb#L36

...you will see that this just calls (as I've mentioned elsewhere) to find with a type of :button, which in turn passes through to Capybara's Query engine which, in turn, ends up just using the standard internal selection mechanism to find things. It's quite elegant; in the same way that an external client can add their own custom selectors to making finding things more convenient:

http://rubydoc.info/github/jnicklas/capybara/master/Capybara#add_selector-class_method

...so Capybara adds its own selectors internally, including, importantly, :button:

https://github.com/jnicklas/capybara/blob/a94dfbc4d07dcfe53bbea334f7f47f584737a0c0/lib/capybara/selector.rb#L133

It's not done by any special case magic, just some predefined custom selectors. Thus, if you've been wondering what custom selectors are available from the get-go in Capybara, that's the file to read (it's probably buried in the docs too but I've not found the list myself yet).

Here, we see that the button code is actually calling XPath::HTML.button, which is a different chunk of code in a different repository, with this documentation:

http://rdoc.info/github/jnicklas/xpath/XPath/HTML#button-instance_method

...which is at the time of writing slightly out of date with respect to the code, since the code shows quite a lot more stuff being recognised, including input types of reset and button (i.e. <input type="button"...> rather than <button...>...</button>, though the latter is also included of course).

https://github.com/jnicklas/xpath/blob/59badfa50d645ac64c70fc6a0c2f7fe826999a1f/lib/xpath/html.rb#L22

We can also see in this code that the finder method really only finds by id, value and title - i.e. not by "text" and not by name either.

So assuming XPath is behaving as intended, though it's not clear from docs, we can see that Capybara isn't documenting itself correctly but probably ought to make the link down to XPath APIs for more information, to avoid the current duplication of information and the problems this can cause for both maintainers and API clients.

In the mean time, I've filed this issue:

https://github.com/jnicklas/capybara/issues/1267


You can also use css selectors which are default capybara locators. People say they are faster.

find('[name=commit]').click

Capybara do not look at name attribute in it's finders :(


You can use xpath selector if you want

find(:xpath, "//input[contains(@name, 'commit')]").click()