Case Insensitive Xpath Contains() Possible

Case insensitive XPath contains() possible?

This is for XPath 1.0. If your environment supports XPath 2.0, see here.


Yes. Possible, but not beautiful.

/html/body//text()[
contains(
translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),
'test'
)
]

This would work for search strings where the alphabet is known beforehand. Add any accented characters you expect to see.


If you can, mark the text that interests you with some other means, like enclosing it in a <span> that has a certain class while building the HTML. Such things are much easier to locate with XPath than substrings in the element text.

If that's not an option, you can let JavaScript (or any other host language that you are using to execute XPath) help you with building an dynamic XPath expression:

function xpathPrepare(xpath, searchString) {
return xpath.replace("$u", searchString.toUpperCase())
.replace("$l", searchString.toLowerCase())
.replace("$s", searchString.toLowerCase());
}

xp = xpathPrepare("//text()[contains(translate(., '$u', '$l'), '$s')]", "Test");
// -> "//text()[contains(translate(., 'TEST', 'test'), 'test')]"

(Hat tip to @KirillPolishchuk's answer - of course you only need to translate those characters you're actually searching for.)

This approach would work for any search string whatsoever, without requiring prior knowledge of the alphabet, which is a big plus.

Both of the methods above fail when search strings can contain single quotes, in which case things get more complicated.

How to perform case insensitive search in XPath?

VBScript supports only XPath 1.0 and not XQuery, so first edit your question title.

In XPath 1.0 the translate() function is used for case insensitivity.

//*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') , search)]/ancestor-or-self::*/*[local-name()='home' and @locale='en']

Where search = Lcase(V_SAEARCH)

It will work perfect. No need to use quotes around your variable.

another way to write this is:-

//*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') , translate('" & search & "', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))]/ancestor-or-self::*/*[local-name()='home' and @locale='en']

Here search variable is being translated in XPath.

case-insensitive matching in XPath?

XPath 2 has a lower-case (and upper-case) string function. That's not quite the same as case-insensitive, but hopefully it will be close enough:

//CD[lower-case(@title)='empire burlesque']

If you are using XPath 1, there is a hack using translate.

How to find elements through text ignoring the case using Selenium and Xpath

To create a list of elements within an webpage containing the text error ignoring the upper/lower cases you can use the translate() function within an xpath as follows:

  • Syntax:

    translate('some text','abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')
  • Line of Code:

    List<WebElement> elementList = driver.findElements(By.xpath("//*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'error')]"));

How to match element by text ignoring case?

Works perfectly:

/html/body//text()[contains(translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'test')]

Source

How do i make Xpath search case insensitive

XPath 1.0 :

$qry = "//channel/item[contains(
translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),
translate($search, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'))]"

XPath 2.0 :

$qry = "//channel/item[lower-case(.) = lower-case($search)]"

Both replace all upper case to lower case.



Related Topics



Leave a reply



Submit