如果我使用以下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>
答案 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