Hello everyone,
Quick question xpath, I need to find the insensitive syntax of my contains
I’m forced to use a label in my xpath, but it can change shape, so I would like to find a way to make case insensitive my contains which works very well but… which is case sensitive:-)
I found different ways with translate and all the alphabet or transformation functions is long and not simple, but the example below pleases me well in its simplicity of use. but I can’t find the syntax
Do you have an idea?
Click Element ${MyMainMenu}//*[contains(text(), ‘SEARCH’)] => Works fine but sensitive
Click Element ${MyMainMenu}//*[matches(text(), ‘${MenuLabelToChoose}’, ‘i’)]
InvalidSelectorException: Message:
Given xpath expression […]//*[matches(text(), “Search”, “i”)]" is invalid:
SyntaxError: Document.evaluate: The expression is not a legal expression
A quick search show methods for making xpath insensitive noting that support of xpath 2.0+ features could vary depending on the browser. (I did a very quick search for cross browser support for xpath 2.0 and didn’t find any reliable information. So leave that for the reader to do …)
I am curious. Is the element completely unfindable by other means? When I get stuck trying to find an element by text I usually leverage Get Webelements and search the returned element that matches the text.
Something like…
‘
${buttons} Get Webelements css:button
FOR ${button} IN @{buttons}
${button_text} Get Text ${button}
${button_text_lower Convert To Lowercase ${button_text}
IF ‘${button_text_lower}’ == ‘search’
Click ${button}
Exit For Loop
END
END
‘
This is a pure RF example, I would probably leverage more python, but I wanted to outline the idea.
and if you put this in a keyword then you have a nice little custom keyword.
This method of gathering Webelements and iterating over it to find the correct one is something I learned years and years ago. It works really well and it is very flexible. Depending upon the loading time of the page it can be a little tricky, I usually throw in a waiter beforehand.
In fact, even in the firefox console I can’t apply this method
$x(“//app-toolbar//mi-toolbar//[contains(@class, ‘main-menu’)]//[contains(text(),‘SEARCH’)]”)
=> OK
$x(“//app-toolbar//mi-toolbar//[contains(@class, ‘main-menu’)]//[matches(text(),‘SEARCH’)]”)
=> error DOM
I am looking for an official statement by the Firefox/Gecko team but from reading the list of xpath functions and noting matches in not in that list I suspect that it only supports xpath 1.0.
As Kelby mentions there are many ways to tackle an automation task and this one may be one where xpath is not the (best) solution.
XPath notes that the translate function is not a sufficient solution for case conversion in all languages. A future version of XPath may provide additional functions for case conversion.
However, this is the closest we have at present to a function that can convert a string to uppercase or lowercase.