Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using upper-case and lower-case xpath functions in selenium IDE

I am trying to get a xpath query using the xpath function lower-case or upper-case, but they seem to not work in selenium (where I test my xpath before I apply it).

Example that does NOT work:

//*[.=upper-case('some text')] 

I have no problem locating the nodes I need in complex path and even using aggregated functions, as long as I don't use the upper and lower case.

Has anyone encountered this before? Does it make sense?

Thanks.

like image 586
Aristotelis Avatar asked Oct 26 '09 15:10

Aristotelis


People also ask

Which XPath function transforms character to uppercase?

XPath/XQuery upper-case function.

Is XPath expression case sensitive?

XPath is a case-sensitive language. Keywords in XPath use lowercase characters and are not reserved. Names in XPath expressions are allowed to be the same as language keywords.


1 Answers

upper-case() and lower-case() are XPath 2.0 functions. Chances are your platform supports XPath 1.0 only.

Try:

translate('some text','abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ') 

which is the XPath 1.0 way to do it. Unfortunately, this requires knowledge of the alphabet the text uses. For plain English, the above probably works, but if you expect accented characters, make sure you add them to the list.


In most environments you are using XPath out of a host language of some sort, and can use the host language's capabilities to work around this XPath 1.0 limitation by externally providing upper- and lower-case variants of the search string to translate().

Shown on the example of Python:

search = 'Some Text' lc = search.lower() uc = search.upper()  xpath = f"//p[contains(translate(., '{lc}', '{uc}'), '{uc}')]" 

This would produce the following XPath expression:

//p[contains(translate(., 'some text', 'SOME TEXT'), 'SOME TEXT')] 

which searches case-insensitively and works for arbitrary search text.

like image 197
Tomalak Avatar answered Oct 01 '22 02:10

Tomalak