Capybara: Ambiguous match, found 2 elements matching xpath "/html"

Created on 21 Nov 2012  路  22Comments  路  Source: teamcapybara/capybara

I'm using capybara (2.0.1) and I'm getting this issue whenever I write an expectation such as:

page.has_content?

However, everything works if I rewrite this to:

find('body').has_content?

I'm not sure if this is the intended functionality or if I've misunderstood the new changes.

Most helpful comment

Done some more debugging on the issue. Did a sanity check and there aren't multiple HTML tags on the page, but it seems like having a doctype of <!DOCTYPE html> is causing the issue. After removing the doctype everything passes.

All 22 comments

Sounds like you have multiple HTML tags on your page. As far as I know that's not allowed by the HTML spec. See: http://dev.w3.org/html5/spec/single-page.html#the-html-element

Done some more debugging on the issue. Did a sanity check and there aren't multiple HTML tags on the page, but it seems like having a doctype of <!DOCTYPE html> is causing the issue. After removing the doctype everything passes.

@tonydaly that should not affect anything.

Nokogiri::HTML("<!DOCTYPE html><html></html").xpath("/html").size # => 1

@tonydaly, maybe your closing </html> tag is missing the slash? Though experimenting with Nokogiri doesn't appear to yield those kinds of results either, what driver are you using?

Erugh ignore this. There was a tag outside of the closing </html>.

@tonydaly thanks for following up. Had the same thing because another dev put extra stuff in there and it would have been the last thing I checked (Literally, it's at the end of the file!). :clap:

For me it's this one causing the trouble:

<!DOCTYPE html>
<!--[if lt IE 7]><html class="lt-ie7"> <![endif]-->

<html>
...
</html>

As you can see even this parser recognizes that the multiple <html> tags are just comments and Capybara maybe shouldn't raise an Exception :) I'm using 2.1.0

Ah, my bad. There IS another between all those comments I have :)

@tonydaly nice catch. I added some JavaScript outside of the trailing HTML tag (in HAML), and it caused the same failure. Thanks for the conversation/fix :+1:

I had a similar problem as @calebhaye, script tag outside of the body tag.

i was using haml....i missed indention right after %hmtl that caused it.

I have the same problem because I am using Bullet gem. Bullet gem appends content outside the html make the html malformed.

@tonydaly (y) my javascript code was outside the html tag. (As slim works with indentation, so after indenting my code inside html it is fine)

Just had this issue and I too had a

Just in case someone hits this problem while debugging a Rails App: The Rails web console was causing it in my case. When I took console out of my controller, the tests went green again.

@TStrothjohann thanks buddy, you save my day.

@TStrothjohann That was my problem too, upgrading web-console fixes this. See this web-console issue.

I had the same problem. Thanks for sharing the solution.

Thanks so much for the discussion here. It helped point me in the right direction. I found that my application.html.slim file was improperly nested. That is there were some elements outside of the html open and closing tags.

For me it's this one causing the trouble:

<!DOCTYPE html>
<!--[if lt IE 7]><html class="lt-ie7"> <![endif]-->

<html>
...
</html>

As you can see even this parser recognizes that the multiple <html> tags are just comments and Capybara maybe shouldn't raise an Exception :) I'm using 2.1.0

https://html.com/tags/doctype/#HTML5_Documents

@chabgood I have no clue why you're adding a link about doctype to a comment from 6 years ago - I'm locking this issue. If you have an actual issue please create a new one with enough details to reproduce it.

Was this page helpful?
0 / 5 - 0 ratings