Search code examples
rubyruby-on-rails-4capybararspec-rails

Cannot find link using Capybara and Rspec while writing an integration test


I am trying to find a link by CSS classes and ids but always getting an error: Capybara::ElementNotFound: Unable to find css ...

The actual piece of code is:

find('#bucket_resources_containers > #user_base_widget.widget > 
div.widget_header > div.right.may-
edit.control.button.add.icon.add_options > a.tasksy.options').click

The page source is: enter image description here


Solution

  • You gave us the answer in your comment, the element was not visible.

    Short answer: find_link(selector, visible: :all).click

    As capybara shows in the documentation:

    By default Capybara will only locate visible elements. This is because a real user would not be able to interact with non-visible elements.

    Only locate visible elements is an smart design of capybara, it avoids thinking that a user would be able to find the element.

    The find_link method documentation doesn't help much when finding for hidden links because it only show these options: wait, href, id, title, alt, class:

    #find_link([locator], options = {}) ⇒ Capybara::Node::Element
    

    But you see on the finding documentation there was a visible option:

    find_link('Hello', :visible => :all).visible?
    find_link(class: ['some_class', 'some_other_class'], :visible => :all).visible?
    

    This option visible comes from #all method, where you can see here. It can have these values:

    true - only finds visible elements.
    false - finds invisible and visible elements.
    :all - same as false; finds visible and invisible elements.
    :hidden - only finds invisible elements.
    :visible - same as true; only finds visible elements.
    

    So, in your case, you could use visible: false, if you really mean it to be hidden, or visible: :all if you don't care about the visibility.