Locating Web Elements by label matching

0 votes

Not always you want to take a look at the underlying HTML code. Therefore it can be handy to define the text of the label in front of form fields. The following example defines the use of label matching.

Example HTML:

<label for="lastname">Last Name</label>
<input type="text" id="lastname" />

We can use:

`//label[contains(text(), 'Last Name')]/../input`

It can be a bit tricky as the label can be defined before, after or around the form fields.


Examine your knowledge and play the ‘locator game‘.

Please follow and like us:
Locating Web Elements by index matching
Locating Web Elements by text matching