xPath select找到几个元素而不是一个元素(兄弟姐妹与兄弟姐妹)

时间:2016-06-13 15:06:51

标签: xpath

如果我使用以下xPath:

//*[contains(@class, 'some-label') and contains(., 'MT:Week')]

它让我完全按照预期一个元素(<span>)。到目前为止一切都很好。

我想要的是将<select>放在旁边,但这样做:

//*[contains(@class, 'some-label') and contains(., 'MT:Week')]/following::select

它已经选择两个 <select>元素。如何选择一个而不使用name属性(!)

HTML:

<div>
  <label>
    <span class="some-label">MT:Month</span>
    <select name="month" id="someID">
      <option selected="selected" value="12">March 2015</option>
    </select>
  </label>
</div>

<div>
  <label>
    <span class="some-label">MT:Week</span>
<!-- want to select just the below one -->
    <select class="width-50px" name="monthWeek" id="someOtherID"> 
      <option selected="selected" value="">All</option>
      <option value="CalendarWeek{year=2015, week=9}">9</option>
      <option value="CalendarWeek{year=2015, week=10}">10</option>

    </select>
  </label>
</div>


<div>
  <label>
    <span class="some-label">MT:Type</span>
    <select name="type" id="anotherID">
      <option selected="selected" value="">All</option>
      <option value="someValue">Value 1</option>
      <option value="someValue2">Value 2</option>

    </select>
  </label>
</div>

1 个答案:

答案 0 :(得分:1)

您可以使用[1]获得第一个:

//*[contains(@class, 'some-label') and contains(., 'MT:Week')]/following::select[1]

但您可能只想要同一标签中的那个,所以使用以下兄弟

//*[contains(@class, 'some-label') and contains(., 'MT:Week')]/following-sibling::select