How to parse a lxml.etree._Element object in robot framework?

Hi, I’m using robotframework-ncclient · PyPI to connect to a router using netconf.

when doing a netconf Get, I’m putting the response into a variable and I have figured that the variable content type is lxml.etree_Element. see below:
Evaluating expression <Element {urn:ietf:params:xml:ns:netconf:base:1.0}data at 0x7f9d80ff1200> failed: TypeError: Expression must be string, got Element.

this is the content of the response:
${res} = <Element {urn:ietf:params:xml:ns:netconf:base:1.0}data at 0x7f57567c9980>

I have used a few things to try to get it working but they both give me the same error for example:

${xml_string} = Evaluate lxml.tostring(${res}, encoding='unicode', pretty_print=True) modules=lxml.etree
and
${xml_string} = Evaluate ${res}.xpath("//version-number", namespaces={'nc': 'urn:ietf:params:xml:ns:netconf:base:1.0'})[0].text modules=lxml.etree

This is the usual error I get:

Evaluating expression "lxml.tostring(<Element {urn:ietf:params:xml:ns:netconf:base:1.0}data at 0x7f4d4a5d8dc0>, encoding='unicode', pretty_print=True)" failed: SyntaxError: invalid syntax (<string>, line 1)

and

Evaluating expression '<Element {urn:ietf:params:xml:ns:netconf:base:1.0}data at 0x7feb95edf640>.xpath("//version-number", namespaces={\'nc\': \'urn:ietf:params:xml:ns:netconf:base:1.0\'})[0].text' failed: SyntaxError: invalid syntax (<string>, line 1)

any Ideas on what I’m doing wrong? and how to parse the content of this xml variable?
Thank you!

just figured it out, using keyword “parse XML” is for string variables that has been formatted as an XML.
but as I’m getting an XML native response I had to change it to string using this keyword “Element to String” and then I’m able to use “Get Element” or any other from there.

also it is very useful to import lxml with XML
Library XML use_lxml=True

Regards.

2 Likes