Home > Enterprise >  How can I use XPath contains() for a few values under 1 common class?
How can I use XPath contains() for a few values under 1 common class?

Time:03-17

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  style="height: 16px;">
 <span >  (  </span>
 <span >  trim  </span>
 <span >  (  </span>
 <span >  STRING_1  </span>
 <span >  )  </span> 
 <span >  =  </span> 
 <span >  upper  </span>
 <span >  (  </span>
 <span >  STRING_2  </span>
 <span >  ))  </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

CodePudding user response:

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')]]"

CodePudding user response:

The texts trim and upper can be at any position within the <div ...>.


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_1 and STRING_2:

    "//div[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'STRING_1')][contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'STRING_2')]"
    

Reference

You can find a relevant detailed discussion in:

  • Related