Reputation: 18506
Capybara is not able to find a <p>
tag by it's id in my cucumber test. I'm able to see the element when I save_and_open_page
. But I can't locate it with has_css?
or find
:
pry(#<Object>)> page.html.scan(/notice_sent/).count
=> 1
pry(#<Object>)> page.html.scan(/id=\"notice_sent\"/).count
=> 1
pry(#<Object>)> page.find('#notice_sent')
Capybara::ElementNotFound: Unable to find css "#notice_sent"
from /Users/me/.gem/ruby/2.1.7/gems/capybara-2.4.4/lib/capybara/node/finders.rb:41:in 'block in find'
What am I missing?
Upvotes: 11
Views: 19421
Reputation: 788
The accepted answer is good in most cases, however, there are times that elements may dynamically present; and not just hidden. When using find
and the element is not present we get an error very similar to when it is present, but hidden. How do we handle that case?
Capybara::Node::Finders#all Also known as: find_all
, has the same visibility option as find
, but will not throw an error when there are no matches.
page.all('#notice_sent', visible: :all)
This is very similar to where
vs find
in ActiveRecord, and there are uses for both approaches.
if you want to return the matching element or nil
instead of an Array / collection, then you can append first
and get a result that closer matches the behavior of find
without the error.
page.all('#notice_sent', visible: :all).first
Upvotes: 0
Reputation: 103
Though I am a newbie to Capybara, I think this might work for you !!
page.find('#notice_sent', :visible => false)
Also, add this code to your env.rb
file.
Capybara.ignore_hidden_elements = false
Upvotes: 1
Reputation: 49850
By default Capybara doesn't find elements that are not visible on the page. You can try
page.find('#notice_sent', visible: :all)
to see if that's the case. If so, and you're testing an app, then you should perform whatever actions a user would perform that would make that element visible, and then check for its presence.
Upvotes: 21
Reputation: 1375
I think, it is time to update capybara for the very beginning.
And, I can not test it out now, but try page.html.find('#notice_sent')
Upvotes: -2