Running click_link 'Title View' against
<a id="title-view-link">Title View</a>
results in Capybara::ElementNotFound: no link with title, id or text 'Title View' found . Adding an empty href -
<a id="title-view-link" href="">Title View</a>
works.
So, is the code testing for links by looking for a elements with href attributes? If so, shouldn't it drop the href attribute as mandatory?
a tags without an href are not links, they are placeholders for links. That's how the HTML spec defines it, that's how every modern browser treats them. Capybara does indeed only click on links which have the href attribute, and imho, that's sensible behaviour.
Yes, agreed. I read the HTML 5 spec about it.
Perhaps a better approach would be something like
<span id='foo'>bar</span>
where there'd be some jquery eventing on the span element, but how would capybara simulate clicking/selecting that text?
For the moment I've lapsed into using href="#", which feels like a bad practice but it's working.
Thanks for response and capybara - great stuff.
I have never tried this, but I think you might be able to do find(:css, '#foo').click.
Jo is right, that should do the trick. Personally, I'd just add the href="#" to the links, I don't think that's bad practice at all.
Adding href="#" is the right concept, but is actually bad practice for the general case:
http://www.javascripttoolbox.com/bestpractices/#onclick
Unless you intend to use the effect of href="#", which is to force the user to jump back to the top of the page if they are below the fold, it should always be avoided. Also when used in conjunction with the <base> tag, these href="#" links will navigate away from the current page to the base URL.
The 'best-practice' syntax is:
<a href="javascript_required.html?doSomething" onclick="doSomething(); return false;">go</a>
Another way to do this is to first find link than "click" on it with keyboard
find_link('id_or_link_anchor').native.send_keys([:return])
Hope it helps somebody.
Regards
@jnicklas I've tried adding href='#' to my links but I run into the same issue as @pboling - my pages jump to the top when the user clicks them. Without a hack like @tomazzlender suggested, Capybara won't click something that isn't a link, and using inline javascript as @pboling suggested mixes your concerns and is generally frowned upon these days.
What I've finally settled on is having a real link that is used if a browser doesn't support JS, and using preventDefault() to keep the link from actually being followed if the browser does support JS.
But I actually got the practice of using a without the href from the Jquery UI documentation (http://jqueryui.com/sortable/). If Jquery UI is using this practice, it seems to me that it's standard enough to support it.
Thanks for all your good work on Capybara.
@michaelrkn standard is kind of a binary concept, it either is standard or it isn't. In this case, standard means the HTML5 spec. This is the relevant section:
If the a element has an href attribute, then it represents a hyperlink (a hypertext anchor) labeled by its contents.
If the a element has no href attribute, then the element represents a placeholder for where a link might otherwise have been placed, if it had been relevant, consisting of just the element's contents.
I don't think this can be much clearer. "then it represents a hyperlink". otherwise is does not.
Capybara will happily click on anything you can throw at it, by the way, it just won't _find_ it as a link, if you do click_link. find("a", :text => "whatever").click will work just fine, href or not.
find("a", :text => "whatever").click :thumbsup:
Thank you
We've just stumbled upon exactly the same issue. However,
find(:css, "id-of-a-tag").clickdoes _not_ work for us. We're using Selenium as web driver (and Javascript driver).
@anpr it needs to be a valid CSS selector
find(:css, "#id-of-a-tag").click
We do that - sorry that my comment wasn't clear on that part. To reiterate: we have a case where
find(:css, "#id-of-a-tag").clickdoesn't work.
@anpr ok, then a stack trace would be helpful, and probably opening a new issue since this one specifically has to do with click_link
@twalpole: In the meantime, we found out that there was a bug so that the link-without-href couldn't be clicked upon. So sorry for the spam, there's no Capybara issue here.
find("a", :title => "whatever").click
Does not seem to work? Is title a valid option? If not, why?
@jmuheim
@jnicklas your suggestion of find("a", :text => "whatever").click is nice -- however I think I'm finding that capybara will visit "/" if it doesn't find a href.
@jjb yep - apparently theres a bug there
Seems it works on my Mac but failed on my Linux box with Firefox. On Linux it went to '/'.
@terryyin This is an old closed issue - if you have a reproducible example of a problem please create a new issue with enough detail to reproduce
Most helpful comment
find("a", :text => "whatever").click:thumbsup:Thank you