'How can I use XPath contains() for a few values under 1 common class?
I need create a Xpath for an expression like -
(trim(STRING_1)=upper(STRING_2))
but unfortunately this expression is located in some editor, the code is -
<div class="ace_line" style="height: 16px;">
<span class="ace_paren ace_lparen"> ( </span>
<span class="ace_support ace_function"> trim </span>
<span class="ace_paren ace_lparen"> ( </span>
<span class="ace_storage ace_type"> STRING_1 </span>
<span class="ace_paren ace_rparen"> ) </span>
<span class="ace_keyword ace_operator"> = </span>
<span class="ace_support ace_function"> upper </span>
<span class="ace_paren ace_lparen"> ( </span>
<span class="ace_storage ace_type"> STRING_2 </span>
<span class="ace_paren ace_rparen"> )) </span></div>
(extra spaces for best visualization)
How can I create a Xpath for the whole expression?
It should be something like -
//*[contains(text(),'(', 'trim', '(', 'STRING_1', ')', '=', 'upper', '(', 'STRING_2', '))')]
But it doesn't work
Solution 1:[1]
You can use multiple contains() in a single XPath expression like:
"//*[contains(text(),'(') and contains(text(),'trim') and contains(text(),'STRING_1') and contains(text(),'=') and contains(text(),'upper') and contains(text(),'STRING_2') and contains(text(),')') and contains(text(),'))')]"
You can also use something like this:
"//div[.//span[contains(text(), 'trim') and .//span[contains(text(), 'upper')]]"
Solution 2:[2]
The texts trim and upper can be at any position within the <div class="ace_line"...>.
Solution
You can use the following locator strategy:
xpath using the texts trim and upper:
"//div[@class='ace_line'][.//span[contains(., 'trim')]][.//span[contains(., 'upper')]]"xpath ignoring the case of
STRING_1:"//div[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'STRING_1')]"xpath ignoring the case of
STRING_1andSTRING_2:"//div[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'STRING_1')][contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'STRING_2')]"
Reference
You can find a relevant detailed discussion in:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
