How to use xpath toI find a label attribute in an arbitrary place

12,220

Solution 1

If your label is associated with an input I would suggest using "for" attribute to search it.

e.g : //label[@for='input_id']

I would discourage the user of text label if you want only one element. Take in account also that label text can be changed often and they are language dependent.

Hope this helps

Solution 2

Maybe a misunderstanding, but possibly an answer - in case it's about a <label>-element like e.g.

<label for="first">First Name</label>

the XPath //label/text() gets the value - First Name - , the XPath //label the whole label-element <label for="first">First Name</label>. In case you mean something else, please provide more details in your question.

Share:
12,220
mattman88
Author by

mattman88

Updated on June 04, 2022

Comments

  • mattman88
    mattman88 about 2 years

    I need to be able to find a label attribute using xpath when the label is buried arbitrarily deep in the HTML. I need to be able to just say something like //*[label] and have it find the first label in the HTML, but it won't. Is there any way to do this? Hopefully my questions was descriptive enough. Thanks.