Note: You are looking at a static snapshot of documentation related to Robot Framework automations. The most recent documentation is at https://robocorp.com/docs

XML

Adds a child element to the specified element.

Arguments

ArgumentTypeDefault value
sourcenull
elementnull
indexNone
xpath.

The element to whom to add the new element is specified using source and xpath. They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.

The element to add can be specified as a path to an XML file or as a string containing XML, or it can be an already parsed XML element. The element is copied before adding so modifying either the original or the added element has no effect on the other . The element is added as the last child by default, but a custom index can be used to alter the position. Indices start from zero (0 = first position, 1 = second position, etc.), and negative numbers refer to positions at the end (-1 = second last position, -2 = third last, etc.).

Examples using ${XML} structure from Example:

Add Element${XML}<new id="x"><c1/></new>
Add Element${XML}<c2/>xpath=new
Add Element${XML}<c3/>index=1xpath=new
${new} =Get Element${XML}new
Elements Should Be Equal${new}<new id="x"><c1/><c3/><c2/></new>

Use Remove Element or Remove Elements to remove elements.

Clears the contents of the specified element.

Arguments

ArgumentTypeDefault value
sourcenull
xpath.
clear_tailFalse

The element to clear is specified using source and xpath. They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.

Clearing the element means removing its text, attributes, and children. Element's tail text is not removed by default, but that can be changed by giving clear_tail a true value (see Boolean arguments). See Element attributes section for more information about tail in general.

Examples using ${XML} structure from Example:

Clear Element${XML}xpath=first
${first} =Get Element${XML}xpath=first
Elements Should Be Equal${first}<first/>
Clear Element${XML}xpath=html/p/bclear_tail=yes
Element Text Should Be${XML}Text with italics.xpath=html/pnormalize_whitespace=yes
Clear Element${XML}
Elements Should Be Equal${XML}<example/>

Use Remove Element to remove the whole element.

Returns a copy of the specified element.

Arguments

ArgumentTypeDefault value
sourcenull
xpath.

The element to copy is specified using source and xpath. They have exactly the same semantics as with Get Element keyword.

If the copy or the original element is modified afterwards, the changes have no effect on the other.

Examples using ${XML} structure from Example:

${elem} =Get Element${XML}xpath=first
${copy1} =Copy Element${elem}
${copy2} =Copy Element${XML}xpath=first
Set Element Text${XML}new textxpath=first
Set Element Attribute${copy1}idnew
Elements Should Be Equal${elem}<first id="1">new text</first>
Elements Should Be Equal${copy1}<first id="new">text</first>
Elements Should Be Equal${copy2}<first id="1">text</first>

Verifies that the specified attribute is expected.

Arguments

ArgumentTypeDefault value
sourcenull
namenull
expectednull
xpath.
messageNone

The element whose attribute is verified is specified using source and xpath. They have exactly the same semantics as with Get Element keyword.

The keyword passes if the attribute name of the element is equal to the expected value, and otherwise it fails. The default error message can be overridden with the message argument.

To test that the element does not have a certain attribute, Python None (i.e. variable ${NONE}) can be used as the expected value. A cleaner alternative is using Element Should Not Have Attribute.

Examples using ${XML} structure from Example:

Element Attribute Should Be${XML}id1xpath=first
Element Attribute Should Be${XML}id${NONE}

See also Element Attribute Should Match and Get Element Attribute.

Verifies that the specified attribute matches expected.

Arguments

ArgumentTypeDefault value
sourcenull
namenull
patternnull
xpath.
messageNone

This keyword works exactly like Element Attribute Should Be except that the expected value can be given as a pattern that the attribute of the element must match.

Pattern matching is similar as matching files in a shell with *, ? and [chars] acting as wildcards. See the Pattern matching section for more information.

Examples using ${XML} structure from Example:

Element Attribute Should Match${XML}id?xpath=first
Element Attribute Should Match${XML}idc*dxpath=third/second

Verifies that one or more element match the given xpath.

Arguments

ArgumentTypeDefault value
sourcenull
xpath.
messageNone

Arguments source and xpath have exactly the same semantics as with Get Elements keyword. Keyword passes if the xpath matches one or more elements in the source. The default error message can be overridden with the message argument.

See also Element Should Not Exist as well as Get Element Count that this keyword uses internally.

Verifies that no element match the given xpath.

Arguments

ArgumentTypeDefault value
sourcenull
xpath.
messageNone

Arguments source and xpath have exactly the same semantics as with Get Elements keyword. Keyword fails if the xpath matches any element in the source. The default error message can be overridden with the message argument.

See also Element Should Exist as well as Get Element Count that this keyword uses internally.

Verifies that the specified element does not have attribute name.

Arguments

ArgumentTypeDefault value
sourcenull
namenull
xpath.
messageNone

The element whose attribute is verified is specified using source and xpath. They have exactly the same semantics as with Get Element keyword.

The keyword fails if the specified element has attribute name. The default error message can be overridden with the message argument.

Examples using ${XML} structure from Example:

Element Should Not Have Attribute${XML}id
Element Should Not Have Attribute${XML}xxxxpath=first

See also Get Element Attribute, Get Element Attributes, Element Text Should Be and Element Text Should Match.

Verifies that the text of the specified element is expected.

Arguments

ArgumentTypeDefault value
sourcenull
expectednull
xpath.
normalize_whitespaceFalse
messageNone

The element whose text is verified is specified using source and xpath. They have exactly the same semantics as with Get Element keyword.

The text to verify is got from the specified element using the same logic as with Get Element Text. This includes optional whitespace normalization using the normalize_whitespace option.

The keyword passes if the text of the element is equal to the expected value, and otherwise it fails. The default error message can be overridden with the message argument. Use Element Text Should Match to verify the text against a pattern instead of an exact value.

Examples using ${XML} structure from Example:

Element Text Should Be${XML}textxpath=first
Element Text Should Be${XML}${EMPTY}xpath=second/child
${paragraph} =Get Element${XML}xpath=html/p
Element Text Should Be${paragraph}Text with bold and italics.normalize_whitespace=yes

Verifies that the text of the specified element matches expected.

Arguments

ArgumentTypeDefault value
sourcenull
patternnull
xpath.
normalize_whitespaceFalse
messageNone

This keyword works exactly like Element Text Should Be except that the expected value can be given as a pattern that the text of the element must match.

Pattern matching is similar as matching files in a shell with *, ? and [chars] acting as wildcards. See the Pattern matching section for more information.

Examples using ${XML} structure from Example:

Element Text Should Match${XML}t???xpath=first
${paragraph} =Get Element${XML}xpath=html/p
Element Text Should Match${paragraph}Text with * and *.normalize_whitespace=yes

Returns the string representation of the specified element.

Arguments

ArgumentTypeDefault value
sourcenull
xpath.
encodingNone

The element to convert to a string is specified using source and xpath. They have exactly the same semantics as with Get Element keyword.

By default the string is returned as Unicode. If encoding argument is given any value, the string is returned as bytes in the specified encoding. The resulting string never contains the XML declaration.

See also Log Element and Save XML.

Verifies that the given source element is equal to expected.

Arguments

ArgumentTypeDefault value
sourcenull
expectednull
exclude_childrenFalse
normalize_whitespaceFalse

Both source and expected can be given as a path to an XML file, as a string containing XML, or as an already parsed XML element structure. See introduction for more information about parsing XML in general.

The keyword passes if the source element and expected element are equal. This includes testing the tag names, texts, and attributes of the elements. By default also child elements are verified the same way, but this can be disabled by setting exclude_children to a true value (see Boolean arguments).

All texts inside the given elements are verified, but possible text outside them is not. By default texts must match exactly, but setting normalize_whitespace to a true value makes text verification independent on newlines, tabs, and the amount of spaces. For more details about handling text see Get Element Text keyword and discussion about elements' text and tail attributes in the introduction.

Examples using ${XML} structure from Example:

${first} =Get Element${XML}first
Elements Should Be Equal${first}<first id="1">text</first>
${p} =Get Element${XML}html/p
Elements Should Be Equal${p}<p>Text with <b>bold</b> and <i>italics</i>.</p>normalize_whitespace=yes
Elements Should Be Equal${p}<p>Text with</p>excludenormalize

The last example may look a bit strange because the <p> element only has text Text with. The reason is that rest of the text inside <p> actually belongs to the child elements. This includes the . at the end that is the tail text of the <i> element.

See also Elements Should Match.

Verifies that the given source element matches expected.

Arguments

ArgumentTypeDefault value
sourcenull
expectednull
exclude_childrenFalse
normalize_whitespaceFalse

This keyword works exactly like Elements Should Be Equal except that texts and attribute values in the expected value can be given as patterns.

Pattern matching is similar as matching files in a shell with *, ? and [chars] acting as wildcards. See the Pattern matching section for more information.

Examples using ${XML} structure from Example:

${first} =Get Element${XML}first
Elements Should Match${first}<first id="?">*</first>

See Elements Should Be Equal for more examples.

Evaluates the given xpath expression and returns results.

Arguments

ArgumentTypeDefault value
sourcenull
expressionnull
context.

The element in which context the expression is executed is specified using source and context arguments. They have exactly the same semantics as source and xpath arguments have with Get Element keyword.

The xpath expression to evaluate is given as expression argument. The result of the evaluation is returned as-is.

Examples using ${XML} structure from Example:

${count} =Evaluate Xpath${XML}count(third/*)
Should Be Equal${count}${3}
${text} =Evaluate Xpath${XML}string(descendant::second[last()]/@id)
Should Be Equal${text}child
${bold} =Evaluate Xpath${XML}boolean(preceding-sibling::*[1] = 'bold')context=html/p/i
Should Be Equal${bold}${True}

This keyword works only if lxml mode is taken into use when importing the library.

Returns the child elements of the specified element as a list.

Arguments

ArgumentTypeDefault value
sourcenull
xpath.

The element whose children to return is specified using source and xpath. They have exactly the same semantics as with Get Element keyword.

All the direct child elements of the specified element are returned. If the element has no children, an empty list is returned.

Examples using ${XML} structure from Example:

${children} =Get Child Elements${XML}
Length Should Be${children}4
${children} =Get Child Elements${XML}xpath=first
Should Be Empty${children}

Returns an element in the source matching the xpath.

Arguments

ArgumentTypeDefault value
sourcenull
xpath.

The source can be a path to an XML file, a string containing XML, or an already parsed XML element. The xpath specifies which element to find. See the introduction for more details about both the possible sources and the supported xpath syntax.

The keyword fails if more, or less, than one element matches the xpath. Use Get Elements if you want all matching elements to be returned.

Examples using ${XML} structure from Example:

${element} =Get Element${XML}second
${child} =Get Element${element}child

Parse XML is recommended for parsing XML when the whole structure is needed. It must be used if there is a need to configure how XML namespaces are handled.

Many other keywords use this keyword internally, and keywords modifying XML are typically documented to both to modify the given source and to return it. Modifying the source does not apply if the source is given as a string. The XML structure parsed based on the string and then modified is nevertheless returned.

Returns the named attribute of the specified element.

Arguments

ArgumentTypeDefault value
sourcenull
namenull
xpath.
defaultNone

The element whose attribute to return is specified using source and xpath. They have exactly the same semantics as with Get Element keyword.

The value of the attribute name of the specified element is returned. If the element does not have such element, the default value is returned instead.

Examples using ${XML} structure from Example:

${attribute} =Get Element Attribute${XML}idxpath=first
Should Be Equal${attribute}1
${attribute} =Get Element Attribute${XML}xxxpath=firstdefault=value
Should Be Equal${attribute}value

See also Get Element Attributes, Element Attribute Should Be, Element Attribute Should Match and Element Should Not Have Attribute.

Returns all attributes of the specified element.

Arguments

ArgumentTypeDefault value
sourcenull
xpath.

The element whose attributes to return is specified using source and xpath. They have exactly the same semantics as with Get Element keyword.

Attributes are returned as a Python dictionary. It is a copy of the original attributes so modifying it has no effect on the XML structure.

Examples using ${XML} structure from Example:

${attributes} =Get Element Attributes${XML}first
Dictionary Should Contain Key${attributes}id
${attributes} =Get Element Attributes${XML}third
Should Be Empty${attributes}

Use Get Element Attribute to get the value of a single attribute.

Returns and logs how many elements the given xpath matches.

Arguments

ArgumentTypeDefault value
sourcenull
xpath.

Arguments source and xpath have exactly the same semantics as with Get Elements keyword that this keyword uses internally.

See also Element Should Exist and Element Should Not Exist.

Returns all text of the element, possibly whitespace normalized.

Arguments

ArgumentTypeDefault value
sourcenull
xpath.
normalize_whitespaceFalse

The element whose text to return is specified using source and xpath. They have exactly the same semantics as with Get Element keyword.

This keyword returns all the text of the specified element, including all the text its children and grandchildren contain. If the element has no text, an empty string is returned. The returned text is thus not always the same as the text attribute of the element.

By default all whitespace, including newlines and indentation, inside the element is returned as-is. If normalize_whitespace is given a true value (see Boolean arguments), then leading and trailing whitespace is stripped, newlines and tabs converted to spaces, and multiple spaces collapsed into one. This is especially useful when dealing with HTML data.

Examples using ${XML} structure from Example:

${text} =Get Element Text${XML}first
Should Be Equal${text}text
${text} =Get Element Text${XML}second/child
Should Be Empty${text}
${paragraph} =Get Element${XML}html/p
${text} =Get Element Text${paragraph}normalize_whitespace=yes
Should Be Equal${text}Text with bold and italics.

See also Get Elements Texts, Element Text Should Be and Element Text Should Match.

Returns a list of elements in the source matching the xpath.

Arguments

ArgumentTypeDefault value
sourcenull
xpathnull

The source can be a path to an XML file, a string containing XML, or an already parsed XML element. The xpath specifies which element to find. See the introduction for more details.

Elements matching the xpath are returned as a list. If no elements match, an empty list is returned. Use Get Element if you want to get exactly one match.

Examples using ${XML} structure from Example:

${children} =Get Elements${XML}third/child
Length Should Be${children}2
${children} =Get Elements${XML}first/child
Should Be Empty${children}

Returns text of all elements matching xpath as a list.

Arguments

ArgumentTypeDefault value
sourcenull
xpathnull
normalize_whitespaceFalse

The elements whose text to return is specified using source and xpath. They have exactly the same semantics as with Get Elements keyword.

The text of the matched elements is returned using the same logic as with Get Element Text. This includes optional whitespace normalization using the normalize_whitespace option.

Examples using ${XML} structure from Example:

@{texts} =Get Elements Texts${XML}third/child
Length Should Be${texts}2
Should Be Equal@{texts}[0]more text
Should Be Equal@{texts}[1]${EMPTY}

Logs the string representation of the specified element.

Arguments

ArgumentTypeDefault value
sourcenull
levelINFO
xpath.

The element specified with source and xpath is first converted into a string using Element To String keyword internally. The resulting string is then logged using the given level.

The logged string is also returned.

Parses the given XML file or string into an element structure.

Arguments

ArgumentTypeDefault value
sourcenull
keep_clark_notationFalse
strip_namespacesFalse

The source can either be a path to an XML file or a string containing XML. In both cases the XML is parsed into ElementTree element structure and the root element is returned. Possible comments and processing instructions in the source XML are removed.

As discussed in Handling XML namespaces section, this keyword, by default, removes namespace information ElementTree has added to tag names and moves it into xmlns attributes. This typically eases handling XML documents with namespaces considerably. If you do not want that to happen, or want to avoid the small overhead of going through the element structure when your XML does not have namespaces, you can disable this feature by giving keep_clark_notation argument a true value (see Boolean arguments).

If you want to strip namespace information altogether so that it is not included even if XML is saved, you can give a true value to strip_namespaces argument.

Examples:

${root} =Parse XML<root><child/></root>
${xml} =Parse XML${CURDIR}/test.xmlkeep_clark_notation=True
${xml} =Parse XML${CURDIR}/test.xmlstrip_namespaces=True

Use Get Element keyword if you want to get a certain element and not the whole structure. See Parsing XML section for more details and examples.

Removes the element matching xpath from the source structure.

Arguments

ArgumentTypeDefault value
sourcenull
xpath
remove_tailFalse

The element to remove from the source is specified with xpath using the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.

The keyword fails if xpath does not match exactly one element. Use Remove Elements to remove all matched elements.

Element's tail text is not removed by default, but that can be changed by giving remove_tail a true value (see Boolean arguments). See Element attributes section for more information about tail in general.

Examples using ${XML} structure from Example:

Remove Element${XML}xpath=second
Element Should Not Exist${XML}xpath=second
Remove Element${XML}xpath=html/p/bremove_tail=yes
Element Text Should Be${XML}Text with italics.xpath=html/pnormalize_whitespace=yes

Removes attribute name from the specified element.

Arguments

ArgumentTypeDefault value
sourcenull
namenull
xpath.

The element whose attribute to remove is specified using source and xpath. They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.

It is not a failure to remove a non-existing attribute. Use Remove Element Attributes to remove all attributes and Set Element Attribute to set them.

Examples using ${XML} structure from Example:

Remove Element Attribute${XML}idxpath=first
Element Should Not Have Attribute${XML}idxpath=first

Can only remove an attribute from a single element. Use Remove Elements Attribute to remove an attribute of multiple elements in one call.

Removes all attributes from the specified element.

Arguments

ArgumentTypeDefault value
sourcenull
xpath.

The element whose attributes to remove is specified using source and xpath. They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.

Use Remove Element Attribute to remove a single attribute and Set Element Attribute to set them.

Examples using ${XML} structure from Example:

Remove Element Attributes${XML}xpath=first
Element Should Not Have Attribute${XML}idxpath=first

Can only remove attributes from a single element. Use Remove Elements Attributes to remove all attributes of multiple elements in one call.

Removes all elements matching xpath from the source structure.

Arguments

ArgumentTypeDefault value
sourcenull
xpath
remove_tailFalse

The elements to remove from the source are specified with xpath using the same semantics as with Get Elements keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.

It is not a failure if xpath matches no elements. Use Remove Element to remove exactly one element.

Element's tail text is not removed by default, but that can be changed by using remove_tail argument similarly as with Remove Element.

Examples using ${XML} structure from Example:

Remove Elements${XML}xpath=*/child
Element Should Not Exist${XML}xpath=second/child
Element Should Not Exist${XML}xpath=third/child

Removes attribute name from the specified elements.

Arguments

ArgumentTypeDefault value
sourcenull
namenull
xpath.

Like Remove Element Attribute but removes the attribute of all elements matching the given xpath.

Removes all attributes from the specified elements.

Arguments

ArgumentTypeDefault value
sourcenull
xpath.

Like Remove Element Attributes but removes all attributes of all elements matching the given xpath.

Saves the given element to the specified file.

Arguments

ArgumentTypeDefault value
sourcenull
pathnull
encodingUTF-8

The element to save is specified with source using the same semantics as with Get Element keyword.

The file where the element is saved is denoted with path and the encoding to use with encoding. The resulting file always contains the XML declaration.

The resulting XML file may not be exactly the same as the original:

  • Comments and processing instructions are always stripped.
  • Possible doctype and namespace prefixes are only preserved when using lxml.
  • Other small differences are possible depending on the ElementTree or lxml version.

Use Element To String if you just need a string representation of the element.

Sets attribute name of the specified element to value.

Arguments

ArgumentTypeDefault value
sourcenull
namenull
valuenull
xpath.

The element whose attribute to set is specified using source and xpath. They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.

It is possible to both set new attributes and to overwrite existing. Use Remove Element Attribute or Remove Element Attributes for removing them.

Examples using ${XML} structure from Example:

Set Element Attribute${XML}attrvalue
Element Attribute Should Be${XML}attrvalue
Set Element Attribute${XML}idnewxpath=first
Element Attribute Should Be${XML}idnewxpath=first

Can only set an attribute of a single element. Use Set Elements Attribute to set an attribute of multiple elements in one call.

Sets the tag of the specified element.

Arguments

ArgumentTypeDefault value
sourcenull
tagnull
xpath.

The element whose tag to set is specified using source and xpath. They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.

Examples using ${XML} structure from Example:

Set Element Tag${XML}newTag
Should Be Equal${XML.tag}newTag
Set Element Tag${XML}xxxxpath=second/child
Element Should Exist${XML}second/xxx
Element Should Not Exist${XML}second/child

Can only set the tag of a single element. Use Set Elements Tag to set the tag of multiple elements in one call.

Sets text and/or tail text of the specified element.

Arguments

ArgumentTypeDefault value
sourcenull
textNone
tailNone
xpath.

The element whose text to set is specified using source and xpath. They have exactly the same semantics as with Get Element keyword. The resulting XML structure is returned, and if the source is an already parsed XML structure, it is also modified in place.

Element's text and tail text are changed only if new text and/or tail values are given. See Element attributes section for more information about text and tail in general.

Examples using ${XML} structure from Example:

Set Element Text${XML}new textxpath=first
Element Text Should Be${XML}new textxpath=first
Set Element Text${XML}tail=&xpath=html/p/b
Element Text Should Be${XML}Text with bold&italics.xpath=html/pnormalize_whitespace=yes
Set Element Text${XML}slanted!!xpath=html/p/i
Element Text Should Be${XML}Text with bold&slanted!!xpath=html/pnormalize_whitespace=yes

Can only set the text/tail of a single element. Use Set Elements Text to set the text/tail of multiple elements in one call.

Sets attribute name of the specified elements to value.

Arguments

ArgumentTypeDefault value
sourcenull
namenull
valuenull
xpath.

Like Set Element Attribute but sets the attribute of all elements matching the given xpath.

Sets the tag of the specified elements.

Arguments

ArgumentTypeDefault value
sourcenull
tagnull
xpath.

Like Set Element Tag but sets the tag of all elements matching the given xpath.

Sets text and/or tail text of the specified elements.

Arguments

ArgumentTypeDefault value
sourcenull
textNone
tailNone
xpath.

Like Set Element Text but sets the text or tail of all elements matching the given xpath.