Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

click_link 'foo' not working on links without href #379

Closed
nicholasf opened this issue Jun 10, 2011 · 20 comments
Closed

click_link 'foo' not working on links without href #379

nicholasf opened this issue Jun 10, 2011 · 20 comments

Comments

@nicholasf
Copy link

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?

@jnicklas
Copy link
Collaborator

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.

@nicholasf
Copy link
Author

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.

@joliss
Copy link
Collaborator

joliss commented Jun 11, 2011

I have never tried this, but I think you might be able to do find(:css, '#foo').click.

@jnicklas
Copy link
Collaborator

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.

@pboling
Copy link

pboling commented Jun 24, 2011

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>

@tomazzlender
Copy link

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

@michaelrkn
Copy link
Contributor

@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.

@jnicklas
Copy link
Collaborator

jnicklas commented Mar 7, 2013

@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.

@Petercopter
Copy link

find("a", :text => "whatever").click 👍

Thank you

@anpr
Copy link

anpr commented Apr 22, 2013

We've just stumbled upon exactly the same issue. However,

find(:css, "id-of-a-tag").click
does not work for us. We're using Selenium as web driver (and Javascript driver).

@twalpole
Copy link
Member

@anpr it needs to be a valid CSS selector

find(:css, "#id-of-a-tag").click

@anpr
Copy link

anpr commented Apr 22, 2013

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").click
doesn't work.

@twalpole
Copy link
Member

@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

@anpr
Copy link

anpr commented Apr 22, 2013

@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.

@jmuheim
Copy link

jmuheim commented Apr 29, 2013

find("a", :title => "whatever").click

Does not seem to work? Is title a valid option? If not, why?

@twalpole
Copy link
Member

@jmuheim

  1. Why would you add this to a closed issue?
  2. After reading the readme why post to an issue on github rather than the mailing list?
  3. No title is not an option because its not needed since the locator will match a title for a link
  4. "Does not seem to work" is a useless description of an issue
  5. find(:link, "whatever").click or click_link("whatever") are 2 options for what you probably want
  6. and if its not actually a link (no href) then you can just use css find("a[title=whatever]").click

@jjb
Copy link

jjb commented Jun 1, 2014

@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.

@twalpole
Copy link
Member

twalpole commented Jun 2, 2014

@jjb yep - apparently theres a bug there

@terryyin
Copy link

Seems it works on my Mac but failed on my Linux box with Firefox. On Linux it went to '/'.

@twalpole
Copy link
Member

@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

@lock lock bot locked and limited conversation to collaborators Aug 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests