XML Library : Get Elements Texts : Syntax to contains by text

Hello everyone,

I’m on version 7.4.1. I’m having a problem with `XML.Get Elements Texts`.

After parsing my file’s XML, I can use

XML.Get Elements Texts ${GlobalXML} suite/test//msg

which returns a lot of informations. So, I’m looking for an `xml.Xpath` to filter it a bit.

If I use

XML.Get Elements Texts ${GlobalXML} suite/test//*[.=‘Slept 1 second’] : It works.

If I use

XML.Get Elements Texts ${GlobalXML} suite/test//*[.=‘Slept’] : Nothing happens because the `=` is strict it s ok.

So, I’m looking for the syntax to use `contains` in the passed XPath.

I tried:

suite/test//*[contains(., 'Slept')]     
and
suite/test//*[contains(text(), 'Slept')]

These syntaxes don’t work : they return an error invalid predicate.

Does anyone have any ideas?

Thank you very much.

Fabrice

Hi Fabrice,

Have you read the top section of the XML Libraries help ? I initially skipped over it like probably most people do, but there is really important information there about how it parses the XML and the two different python modules that can be used for parsing the XML, by default it uses ElementTree, but you can specify to use lxml instead.

From my reading of the matter lxml allows the more advanced xpaths, so perhaps try

Library    XML    use_lxml=true

Maybe that will give you what you need.

Also I forget which one, but one of these python modules needs a .at the beginning of the xpath, which also caught me out.

Hopefully this helps,

Dave.

1 Like

Hi Dave,
Yes, I read the top of the section and you’re right, there’s a lot of information to consider.

I tried `lxml=true` but it says it’s not installed… if you say that changes things, I’ll fix it.
Thanks again.
Fabrice

Hi Fabrice,

Yeah you need to install the lxml module if you don’t already have it.

I wouldn’t go as far as saying fixes, but rather changes, both modules have their pros and cons, but the biggest con of ElementTree is it’s very limited xpath capabilities.

I’ve used both in different situations and I’m no expert on either.

Dave.

1 Like

And…it works!
lxml and the syntax /*[contains(text(), ‘Slept’)] are OK.

Thanks again, Dave.

3 Likes