0

I have a website and want to select some element; here is my XPATH expression:

//span[contains(.,"Arbeitsräume")]

which returns two matches. The first match probably comes from this html code piece:

 <span class="menu-item-text">                       
     <i class="fa fa-users"></i>
     Arbeitsräume
 </span>

The second match I do not know. However, I want to select the first match. Therefore I tried:

//span[contains(.,"Arbeitsräume")][1]

which again gave TWO matches. A '0' or a '2' in the bracket returned no matches.

I though a square bracket at the end of the xpath expression lets you select the first, second, ... match, but I might be mistaken.

So how can I select the first, second... XPATH match?

Alex
  • 41,580
  • 88
  • 260
  • 469

1 Answers1

1

Try

(//span[contains(.,"Arbeitsräume")])[1]

The expression //span[1] selects the first span child of every element containing spans. For example, given the following document

<doc>
    <div>
        <span id="1.1"/>
        <span id="1.2"/>
    </div>
    <div>
        <span id="2.1"/>
        <span id="2.2"/>
    </div>
</doc>

two span elements with ids 1.1 and 2.1 are selected. (//span)[1] on other hand only returns the first span element in the whole document.

nwellnhof
  • 32,319
  • 7
  • 89
  • 113