I’m unable to make xpath to work with capybara even for simple scenarios, e.g. I’ve got:
<div id="drawer-container" role="application" class="drawers-2">
<div class="drawer" id="drawer-1">
<h2 role="tab" aria-controls="tabpanel-highlights">
<a href="#" class="drawer-toggle" role="button">
<span>Featured</span>
<span class="context blq-hide">section collapsed</span>
</a>
</h2>
but when I try the following with Capybara:
find(:xpath, "div[@id='drawer-1' and @class='drawer']").click
Then it fails saying:
Unable to find xpath "div[@id='drawer-1' and @class='drawer']" (Capybara::ElementNotFound)
I read that it might be due to webrat, but I don’t have that gem. My Capybara version is 2.0.1
Any idea what could be causing this issue?
Thank you!
The xpath has to state where to start from. You likely want to locate the div tag no matter where it is in the document, so you should start the xpath with “//”:
Note that while the above xpath will fix your exception, you may not get the results you want. You probably want to click the link instead: