无法找到optgroup或选择带有水豚的标签

时间:2015-09-04 10:01:04

标签: xpath rspec capybara

我使用Capybara(put body.page)获得以下HTML-Structur

    <div class="analyze-select" id="select-project">
      <label class="label" for="project_id_Select Project">Select project</label>
<select class="chosen-select" data-placeholder="Project" id="select_project_id" name="select[project_id]" style="width: 150px; display: none;">
      <option value=""></option>
      <optgroup label="Active">
        <option value="1">Project-0</option>
        <option value="2">Project-1</option>
      </optgroup>
      <optgroup label="Inactive">
        <option value="6">deactivated project 3</option>
      </optgroup>
    </select>
  </div>

当我尝试

page.should have_xpath("//div[@id='select-project']") # => true
page.should have_xpath("//label[@for='project_id_Select Project']") # => true 
page.should have_xpath("//select[@class='chosen-select']") # => false
page.should have_xpath("//optgroup") # => false

为什么找不到select-或optgroup-tag?

2 个答案:

答案 0 :(得分:2)

对于select,您可以使用带有可见选项的have_select节点匹配器。 &#34;假&#34;将检查页面中的可见和不可见元素。

page.should have_select(&#34; id / name / label&#34;,visible:false)

答案 1 :(得分:0)

隐藏的选定值:

expect(page).to have_select('id/name/label', selected: 'OPTION_TEXT', visible: false)

谢谢@ sohini-chandra。