Capybara:找不到xpath

时间:2012-12-12 00:31:49

标签: capybara

即使对于简单的场景,我也无法使xpath与capybara一起工作,例如我有:

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

但是当我在Capybara尝试以下内容时:

find(:xpath, "div[@id='drawer-1' and @class='drawer']").click

然后它说不出来:

Unable to find xpath "div[@id='drawer-1' and @class='drawer']" (Capybara::ElementNotFound)

我读到它可能是由于webrat,但我没有那个宝石。我的Capybara版本是2.0.1

知道可能导致此问题的原因是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

xpath必须说明从哪里开始。您可能希望找到div标签,无论它在文档中的位置,因此您应该使用“//”启动xpath:

find(:xpath, "//div[@id='drawer-1' and @class='drawer']").click

请注意,虽然上述xpath将修复您的异常,但您可能无法获得所需的结果。您可能想要点击链接:

find(:xpath, "//div[@id='drawer-1' and @class='drawer']/h2/a").click