1

I have a query and I'd like it to find any match on a page - regardless if any of the letters on the page are upper or lower case.

My query:

//*[contains(text(),'Deez')]

I've tried the solutions I've seen given to other similar questions but none have worked. My query uses text() which I've not seen in the other questions. Is that a problem?

Toms Code
  • 1,439
  • 3
  • 15
  • 34

1 Answers1

1

With XPath 2.0 or greater, you can use upper-case():

//*[contains(upper-case(text()),'DEEZ')]

or lower-case():

//*[contains(lower-case(text()),'deez')]

or matches() with the case insensitive flag i (won't be the most efficient):

//*[matches(text(),'Deez', 'i')]

For XPath 1.0 and greater, you can use translate() to ensure that all the letters are upper or lower-case:

//*[contains(translate(text(), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),'DEEZ')]
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • Thanks for such a thorough answer Mads. The last one worked for me in Chrome (I presume they are using XPath 1.0 or similar! – Toms Code Dec 13 '22 at 22:01
  • 1
    one quick thing Mads, you missed a letter in your XPath 1.0 answer but I can't edit it as theres too many edits in the stackoverflow queue or something // *[contains(translate(text(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'), 'Deez')] – Toms Code Dec 13 '22 at 22:41