RobotFramework之XML
时间: 2018-08-15来源:OSCHINA
前景提要
「深度学习福利」大神带你进阶工程师,立即查看>>>
XML
Library version: 3.0.4
Library scope: global
Named arguments: supported

Introduction
Robot Framework test library for verifying and modifying XML documents.
As the name implies, XML is a test library for verifying contents of XML files. In practice it is a pretty thin wrapper on top of Python's ElementTree XML API .
The library has the following main usages: Parsing an XML file, or a string containing XML, into an XML element structure and finding certain elements from it for for further analysis (e.g. Parse XML and Get Element keywords). Getting text or attributes of elements (e.g. Get Element Text and Get Element Attribute ). Directly verifying text, attributes, or whole elements (e.g Element Text Should Be and Elements Should Be Equal ). Modifying XML and saving it (e.g. Set Element Text , Add Element and Save XML ).
Table of contents Parsing XML Using lxml Example Finding elements with xpath Element attributes Handling XML namespaces Boolean arguments Shortcuts Keywords
Parsing XML
XML can be parsed into an element structure using Parse XML keyword. It accepts both paths to XML files and strings that contain XML. The keyword returns the root element of the structure, which then contains other elements as its children and their children. Possible comments and processing instructions in the source XML are removed.
XML is not validated during parsing even if has a schema defined. How possible doctype elements are handled otherwise depends on the used XML module and on the platform. The standard ElementTree strips doctypes altogether but when using lxml they are preserved when XML is saved. With IronPython parsing XML with a doctype is not supported at all.
The element structure returned by Parse XML , as well as elements returned by keywords such as Get Element , can be used as the source argument with other keywords. In addition to an already parsed XML structure, other keywords also accept paths to XML files and strings containing XML similarly as Parse XML . Notice that keywords that modify XML do not write those changes back to disk even if the source would be given as a path to a file. Changes must always saved explicitly using Save XML keyword.
When the source is given as a path to a file, the forward slash character ( / ) can be used as the path separator regardless the operating system. On Windows also the backslash works, but it the test data it needs to be escaped by doubling it ( \\ ). Using the built-in variable ${/} naturally works too.
Using lxml
By default this library uses Python's standard ElementTree module for parsing XML, but it can be configured to use lxml module instead when importing the library. The resulting element structure has same API regardless which module is used for parsing.
The main benefits of using lxml is that it supports richer xpath syntax than the standard ElementTree and enables using Evaluate Xpath keyword. It also preserves the doctype and possible namespace prefixes saving XML.
The lxml support is new in Robot Framework 2.8.5.
Example
The following simple example demonstrates parsing XML and verifying its contents both using keywords in this library and in BuiltIn and Collections libraries. How to use xpath expressions to find elements and what attributes the returned elements contain are discussed, with more examples, in Finding elements with xpath and Element attributes sections.
In this example, as well as in many other examples in this documentation, ${XML} refers to the following example XML document. In practice ${XML} could either be a path to an XML file or it could contain the XML itself. <example> <first id="1">text</first> <second id="2"> <child/> </second> <third> <child>more text</child> <second id="child"/> <child><grandchild/></child> </third> <html> <p> Text with <b>bold</b> and <i>italics</i>. </p> </html> </example>
${root} = Parse XML ${XML}
Should Be Equal ${root.tag} example
${first} = Get Element ${root} first
Should Be Equal ${first.text} text
Dictionary Should Contain Key ${first.attrib} id
Element Text Should Be Element Attribute Should Be Element Attribute Should Be Element Attribute Should Be
${first} ${first} ${root} ${XML}
text id id id
1 1 1
xpath=first xpath=first

Notice that in the example three last lines are equivalent. Which one to use in practice depends on which other elements you need to get or verify. If you only need to do one verification, using the last line alone would suffice. If more verifications are needed, parsing the XML with Parse XML only once would be more efficient.
Finding elements with xpath
ElementTree, and thus also this library, supports finding elements using xpath expressions. ElementTree does not, however, support the full xpath syntax, and what is supported depends on its version. ElementTree 1.3 that is distributed with Python 2.7 supports richer syntax than earlier versions.
The supported xpath syntax is explained below and ElementTree documentation provides more details. In the examples ${XML} refers to the same XML structure as in the earlier example.
If lxml support is enabled when importing the library, the whole xpath 1.0 standard is supported. That includes everything listed below but also lot of other useful constructs.
Tag names
When just a single tag name is used, xpath matches all direct child elements that have that tag name.
${elem} = Get Element ${XML} third
Should Be Equal @{children} = Length Should Be
${elem.tag} Get Elements ${children}
third ${elem} 2
child

Paths
Paths are created by combining tag names with a forward slash ( / ). For example, parent/child matches all child elements under parent element. Notice that if there are multiple parent elements that all have child elements, parent/child xpath will match all these child elements.
${elem} = Get Element ${XML} second/child
Should Be Equal ${elem} = Should Be Equal
${elem.tag} Get Element ${elem.tag}
child ${XML} grandchild
third/child/grandchild

Wildcards
An asterisk ( * ) can be used in paths instead of a tag name to denote any element.

@{children} = Length Should Be
Get Elements ${children}
${XML} 3
*/child

Current element
The current element is denoted with a dot ( . ). Normally the current element is implicit and does not need to be included in the xpath.
Parent element
The parent element of another element is denoted with two dots ( .. ). Notice that it is not possible to refer to the parent of the current element. This syntax is supported only in ElementTree 1.3 (i.e. Python/Jython 2.7 and newer).

${elem} = Should Be Equal
Get Element ${elem.tag}
${XML} third
*/second/..

Search all sub elements
Two forward slashes ( // ) mean that all sub elements, not only the direct children, are searched. If the search is started from the current element, an explicit dot is required.
@{elements} = Get Elements ${XML} .//second
Length Should Be ${b} = Should Be Equal
${elements} Get Element ${b.text}
2 ${XML} bold
html//b

Predicates
Predicates allow selecting elements using also other criteria than tag names, for example, attributes or position. They are specified after the normal tag name or path using syntax path[predicate] . The path can have wildcards and other special syntax explained above.
What predicates ElementTree supports is explained in the table below. Notice that predicates in general are supported only in ElementTree 1.3 (i.e. Python/Jython 2.7 and newer).
Predicate Matches Example
@attrib Elements with attribute attrib . second[ @id ]
@attrib="value" Elements with attribute attrib having value value . *[@id="2"]
position tag
Elements at the specified position. Position can be an integer (starting from 1), expression last() , or relative expression like last() - 1 . Elements with a child element named tag .
third/child[1] third/child[grandchild]

Predicates can also be stacked like path[predicate1][predicate2] . A limitation is that possible position predicate must always be first.
Element attributes
All keywords returning elements, such as Parse XML , and Get Element , return ElementTree's Element objects . These elements can be used as inputs for other keywords, but they also contain several useful attributes that can be accessed directly using the extended variable syntax.
The attributes that are both useful and convenient to use in the test data are explained below. Also other attributes, including methods, can be accessed, but that is typically better to do in custom libraries than directly in the test data.
The examples use the same ${XML} structure as the earlier examples.
tag
The tag of the element.

${root} = Should Be Equal
Parse XML ${root.tag}
${XML} example

text
The text that the element contains or Python None if the element has no text. Notice that the text does not contain texts of possible child elements nor text after or between children. Notice also that in XML whitespace is significant, so the text contains also possible indentation and newlines. To get also text of the possible children, optionally whitespace normalized, use Get Element Text keyword.
${1st} = Get Element ${XML} first
Should Be Equal ${1st.text} text
${2nd} = Get Element ${XML} second/child
Should Be Equal ${p} = Should Be Equal
${2nd.text} Get Element ${p.text}
${NONE} ${XML} \n${SPACE*6}Text with${SPACE}
html/p

tail
The text after the element before the next opening or closing tag. Python None if the element has no tail. Similarly as with text , also tail contains possible indentation and newlines.

${b} = Should Be Equal
Get Element ${b.tail}
${XML} ${SPACE}and${SPACE}
html/p/b

attrib
A Python dictionary containing attributes of the element.
${2nd} = Get Element ${XML} second
Should Be Equal ${3rd} = Should Be Empty
${2nd.attrib['id']} Get Element ${3rd.attrib}
2 ${XML}
third

Handling XML namespaces
ElementTree and lxml handle possible namespaces in XML documents by adding the namespace URI to tag names in so called Clark Notation. That is inconvenient especially with xpaths, and by default this library strips those namespaces away and moves them to xmlns attribute instead. That can be avoided by passing keep_clark_notation argument to Parse XML keyword. Alternatively Parse XML supports stripping namespace information altogether by using strip_namespaces argument. The pros and cons of different approaches are discussed in more detail below.
How ElementTree handles namespaces
If an XML document has namespaces, ElementTree adds namespace information to tag names in Clark Notation (e.g. {http://ns.uri}tag ) and removes original xmlns attributes. This is done both with default namespaces and with namespaces with a prefix. How it works in practice is illustrated by the following example, where ${NS} variable contains this XML document: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:template match="/"> <html></html> </xsl:template> </xsl:stylesheet>
${root} = Parse XML ${NS} keep_clark_notation=yes
Should Be Equal Element Should Exist Should Be Empty
${root.tag} ${root} ${root.attrib}
{http://www.w3.org/1999/XSL/Transform}stylesheet {http://www.w3.org/1999/XSL/Transform}template/{http://www.w3.org/1999/xhtml}html


As you can see, including the namespace URI in tag names makes xpaths really long and complex.
If you save the XML, ElementTree moves namespace information back to xmlns attributes. Unfortunately it does not restore the original prefixes: <ns0:stylesheet xmlns:ns0="http://www.w3.org/1999/XSL/Transform"> <ns0:template match="/"> <ns1:html xmlns:ns1="http://www.w3.org/1999/xhtml"></ns1:html> </ns0:template> </ns0:stylesheet>
The resulting output is semantically same as the original, but mangling prefixes like this may still not be desirable. Notice also that the actual output depends slightly on ElementTree version.
Default namespace handling
Because the way ElementTree handles namespaces makes xpaths so complicated, this library, by default, strips namespaces from tag names and moves that information back to xmlns attributes. How this works in practice is shown by the example below, where ${NS} variable contains the same XML document as in the previous example.
${root} = Parse XML ${NS}
Should Be Equal Element Should Exist Element Attribute Should Be Element Attribute Should Be
${root.tag} ${root} ${root} ${root}
stylesheet template/html xmlns xmlns
http://www.w3.org/1999/XSL/Transform http://www.w3.org/1999/xhtml
xpath=template/html

Now that tags do not contain namespace information, xpaths are simple again.
A minor limitation of this approach is that namespace prefixes are lost. As a result the saved output is not exactly same as the original one in this case either: <stylesheet xmlns="http://www.w3.org/1999/XSL/Transform"> <template match="/"> <html xmlns="http://www.w3.org/1999/xhtml"></html> </template> </stylesheet>
Also this output is semantically same as the original. If the original XML had only default namespaces, the output would also look identical.
Namespaces when using lxml
This library handles namespaces same way both when using lxml and when not using it. There are, however, differences how lxml internally handles namespaces compared to the standard ElementTree. The main difference is that lxml stores information about namespace prefixes and they are thus preserved if XML is saved. Another visible difference is that lxml includes namespace information in child elements got with Get Element if the parent element has namespaces.
Stripping namespaces altogether
Because namespaces often add unnecessary complexity, Parse XML supports stripping them altogether by using strip_namespaces=True . When this option is enabled, namespaces are not shown anywhere nor are they included if XML is saved.
Attribute namespaces
Attributes in XML documents are, by default, in the same namespaces as the element they belong to. It is possible to use different namespaces by using prefixes, but this is pretty rare.
If an attribute has a namespace prefix, ElementTree will replace it with Clark Notation the same way it handles elements. Because stripping namespaces from attributes could cause attribute conflicts, this library does not handle attribute namespaces at all. Thus the following example works the same way regardless how namespaces are handled.

${root} = Element Attribute Should Be Element Attribute Should Be
Parse XML ${root} ${root}
<root id="1" ns:id="2" xmlns:ns="http://my.ns"/> id {http://my.ns}id
1 2

Boolean arguments
Some keywords accept arguments that are handled as Boolean values true or false. If such an argument is given as a string, it is considered false if it is either an empty string or case-insensitively equal to false , none or no . Other strings are considered true regardless their value, and other argument types are tested using the same rules as in Python .
True examples:
Parse XML ${XML} keep_clark_notation=True # Strings are generally true.
Parse XML Parse XML Parse XML
${XML} ${XML} ${XML}
keep_clark_notation=yes keep_clark_notation=${TRUE} keep_clark_notation=${42}
# Same as the above. # Python True is true. # Numbers other than 0 are true.

False examples:
Parse XML ${XML} keep_clark_notation=False # String false is false.
Parse XML Parse XML Parse XML
${XML} ${XML} ${XML}
keep_clark_notation=no keep_clark_notation=${EMPTY} keep_clark_notation=${FALSE}
# Also string no is false. # Empty string is false. # Python False is false.

Prior to Robot Framework 2.9, all non-empty strings, including false and no , were considered to be true. Considering none false is new in Robot Framework 3.0.3.
Importing
Arguments Documentation
use_lxml=False
Import library with optionally lxml mode enabled.
By default this library uses Python's standard ElementTree module for parsing XML. If use_lxml argument is given a true value (see Boolean arguments ), the library will use lxml module instead. See Using lxml section for benefits provided by lxml.
Using lxml requires that the lxml module is installed on the system. If lxml mode is enabled but the module is not installed, this library will emit a warning and revert back to using the standard ElementTree.
The support for lxml is new in Robot Framework 2.8.5.

Shortcuts
Add Element · Clear Element · Copy Element · Element Attribute Should Be · Element Attribute Should Match · Element Should Exist · Element Should Not Exist · Element Should Not Have Attribute · Element Text Should Be · Element Text Should Match · Element To String · Elements Should Be Equal · Elements Should Match · Evaluate Xpath · Get Child Elements · Get Element · Get Element Attribute · Get Element Attributes · Get Element Count · Get Element Text · Get Elements · Get Elements Texts · Log Element · Parse Xml · Remove Element · Remove Element Attribute · Remove Element Attributes · Remove Elements · Remove Elements Attribute · Remove Elements Attributes · Save Xml · Set Element Attribute · Set Element Tag · Set Element Text · Set Elements Attribute · Set Elements Tag · Set Elements Text
Keywords
Keyword Arguments Documentation
Add Element source, element,index=None, xpath=. Adds a child element to the specified element.
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 Add Element ${new} = Elements Should Be Equal
${XML} ${XML} Get Element ${new}
<c2/> <c3/> ${XML} <new id="x"><c1/><c3/><c2/></new>
xpath=new index=1 new
xpath=new

Use Remove Element or Remove Elements to remove elements.
Clear Element source, xpath=.,clear_tail=False Clears the contents of the specified element.
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 Element Text Should Be Clear Element Elements Should Be Equal
${XML} ${XML} ${XML} ${XML}
xpath=html/p/b Text with italics. <example/>
clear_tail=yes xpath=html/p
normalize_whitespace=yes

Use Remove Element to remove the whole element.
Copy Element source, xpath=. Returns a copy of the specified element.
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 text xpath=first
Set Element Attribute ${copy1} id new
Elements Should Be Equal Elements Should Be Equal Elements Should Be Equal
${elem} ${copy1} ${copy2}
<first id="1">new text</first> <first id="new">text</first> <first id="1">text</first>


Element Attribute Should Be source, name, expected,xpath=., message=None Verifies that the specified attribute is expected .
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 Element Attribute Should Be
${XML} ${XML}
id id
1 ${NONE}
xpath=first

See also Element Attribute Should Match and Get Element Attribute .
Element Attribute Should Match source, name, pattern,xpath=., message=None Verifies that the specified attribute matches expected .
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, and it is always case-sensitive. In the pattern, '*' matches anything and '?' matches any single character.
Examples using ${XML} structure from Example :

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

Element Should Exist source, xpath=.,message=None Verifies that one or more element match the given xpath .
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.
Element Should Not Exist source, xpath=.,message=None Verifies that no element match the given xpath .
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.
Element Should Not Have Attribute source, name, xpath=.,message=None Verifies that the specified element does not have attribute name .
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 Element Should Not Have Attribute
${XML} ${XML}
id xxx
xpath=first

See also Get Element Attribute , Get Element Attributes , Element Text Should Be and Element Text Should Match .
Element Text Should Be source, expected, xpath=.,normalize_whitespace=False,message=None Verifies that the text of the specified element is expected .
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} text xpath=first
Element Text Should Be ${paragraph} = Element Text Should Be
${XML} Get Element ${paragraph}
${EMPTY} ${XML} Text with bold and italics.
xpath=second/child xpath=html/p normalize_whitespace=yes

Element Text Should Match source, pattern, xpath=.,normalize_whitespace=False,message=None Verifies that the text of the specified element matches expected .
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, and it is always case-sensitive. In the pattern, '*' matches anything and '?' matches any single character.
Examples using ${XML} structure from Example :

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

Element To String source, xpath=.,encoding=None Returns the string representation of the specified element.
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 .
Elements Should Be Equal source, expected,exclude_children=False,normalize_whitespace=False Verifies that the given source element is equal to expected .
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 ${p} = Elements Should Be Equal Elements Should Be Equal
${first} Get Element ${p} ${p}
<first id="1">text</first> ${XML} <p>Text with <b>bold</b> and <i>italics</i>.</p> <p>Text with</p>
html/p normalize_whitespace=yes exclude
normalize

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 .
Elements Should Match source, expected,exclude_children=False,normalize_whitespace=False Verifies that the given source element matches expected .
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, and it is always case-sensitive. In the pattern, '*' matches anything and '?' matches any single character.
Examples using ${XML} structure from Example :

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

See Elements Should Be Equal for more examples.
Evaluate Xpath source, expression,context=. Evaluates the given xpath expression and returns results.
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} = Should Be Equal ${bold} = Should Be Equal
Evaluate Xpath ${text} Evaluate Xpath ${bold}
${XML} child ${XML} ${True}
string(descendant::second[last()]/@id) boolean(preceding-sibling::*[1] = 'bold')
context=html/p/i

This keyword works only if lxml mode is taken into use when importing the library. New in Robot Framework 2.8.5.
Get Child Elements source, xpath=. Returns the child elements of the specified element as a list.
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} = Should Be Empty
${children} Get Child Elements ${children}
4 ${XML}
xpath=first

Get Element source, xpath=. Returns an element in the source matching the 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} = ${child} =
Get Element Get Element
${XML} ${element}
second 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.
Get Element Attribute source, name, xpath=.,default=None Returns the named attribute of the specified element.
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} = Should Be Equal ${attribute} = Should Be Equal
Get Element Attribute ${attribute} Get Element Attribute ${attribute}
${XML} 1 ${XML} value
id xx
xpath=first xpath=first
default=value

See also Get Element Attributes , Element Attribute Should Be , Element Attribute Should Match and Element Should Not Have Attribute .
Get Element Attributes source, xpath=. Returns all attributes of the specified element.
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} = Should Be Empty
${attributes} Get Element Attributes ${attributes}
id ${XML}
third

Use Get Element Attribute to get the value of a single attribute.
Get Element Count source, xpath=. Returns and logs how many elements the given xpath matches.
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 .
Get Element Text source, xpath=.,normalize_whitespace=False Returns all text of the element, possibly whitespace normalized.
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} = ${text} = Should Be Equal
Get Element Get Element Text ${text}
${XML} ${paragraph} Text with bold and italics.
html/p normalize_whitespace=yes

See also Get Elements Texts , Element Text Should Be and Element Text Should Match .
Get Elements source, xpath Returns a list of elements in the source matching the 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.
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} = Should Be Empty
${children} Get Elements ${children}
2 ${XML}
first/child

Get Elements Texts source, xpath,normalize_whitespace=False Returns text of all elements matching xpath as a list.
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 Should Be Equal Should Be Equal
${texts} @{texts}[0] @{texts}[1]
2 more text ${EMPTY}


Log Element source, level=INFO, xpath=. Logs the string representation of the specified element.
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.
Parse Xml source,keep_clark_notation=False,strip_namespaces=False Parses the given XML file or string into an element structure.
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. This functionality is new in Robot Framework 3.0.2.
Examples:

${root} = ${xml} = ${xml} =
Parse XML Parse XML Parse XML
<root><child/></root> ${CURDIR}/test.xml ${CURDIR}/test.xml
keep_clark_notation=True strip_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.
Remove Element source, xpath=,remove_tail=False Removes the element matching xpath from the source structure.
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 Element Should Not Exist Remove Element Element Text Should Be
${XML} ${XML} ${XML} ${XML}
xpath=second xpath=second xpath=html/p/b Text with italics.
remove_tail=yes xpath=html/p
normalize_whitespace=yes

Remove Element Attribute source, name, xpath=. Removes attribute name from the specified element.
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 Element Should Not Have Attribute
${XML} ${XML}
id id
xpath=first xpath=first

Can only remove an attribute from a single element. Use Remove Elements Attribute to remove an attribute of multiple elements in one call.
Remove Element Attributes source, xpath=. Removes all attributes from the specified element.
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 Element Should Not Have Attribute
${XML} ${XML}
xpath=first id
xpath=first

Can only remove attributes from a single element. Use Remove Elements Attributes to remove all attributes of multiple elements in one call.
Remove Elements source, xpath=,remove_tail=False Removes all elements matching xpath from the source structure.
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 Element Should Not Exist
${XML} ${XML}
xpath=second/child xpath=third/child

Remove Elements Attribute source, name, xpath=. Removes attribute name from the specified elements.
Like Remove Element Attribute but removes the attribute of all elements matching the given xpath .
New in Robot Framework 2.8.6.
Remove Elements Attributes source, xpath=. Removes all attributes from the specified elements.
Like Remove Element Attributes but removes all attributes of all elements matching the given xpath .
New in Robot Framework 2.8.6.
Save Xml source, path,encoding=UTF-8 Saves the given element to the specified file.
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.
Set Element Attribute source, name, value,xpath=. Sets attribute name of the specified element to value .
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 Element Attribute Should Be Set Element Attribute Element Attribute Should Be
${XML} ${XML} ${XML} ${XML}
attr attr id id
value value new new
xpath=first xpath=first

Can only set an attribute of a single element. Use Set Elements Attribute to set an attribute of multiple elements in one call.
Set Element Tag source, tag, xpath=. Sets the tag of the specified element.
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 Element Should Exist Element Should Not Exist
${XML} ${XML} ${XML}
xxx second/xxx second/child
xpath=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.
Set Element Text source, text=None,tail=None, xpath=. Sets text and/or tail text of the specified element.
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 text xpath=first
Element Text Should Be ${XML} new text xpath=first
Set Element Text Element Text Should Be Set Element Text Element Text Should Be
${XML} ${XML} ${XML} ${XML}
tail=& Text with bold&italics. slanted Text with bold&slanted!!
xpath=html/p/b xpath=html/p !! xpath=html/p
normalize_whitespace=yes xpath=html/p/i normalize_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.
Set Elements Attribute source, name, value,xpath=. Sets attribute name of the specified elements to value .
Like Set Element Attribute but sets the attribute of all elements matching the given xpath .
New in Robot Framework 2.8.6.
Set Elements Tag Set Elements Text
source, tag, xpath=. source, text=None,tail=None, xpath=.
Sets the tag of the specified elements.
Like Set Element Tag but sets the tag of all elements matching the given xpath .
New in Robot Framework 2.8.6. Sets text and/or tail text of the specified elements.
Like Set Element Text but sets the text or tail of all elements matching the given xpath .
New in Robot Framework 2.8.6.

Altogether 37 keywords.
Generated by Libdoc on 2018-04-25 23:41:29.

XML
图书馆版本: 3.0.4
图书馆范围: 全球
命名参数: 支持的

介绍
Robot Framework测试库,用于验证和修改XML文档。
顾名思义, XML 是用于验证XML文件内容的测试库。实际上,它是Python的 ElementTree XML API 之上的一个非常薄的包装器。
该库具有以下主要用途: 将XML文件或包含XML的字符串解析为XML元素结构,并从中查找某些元素以供进一步分析(例如, Parse XML 和 Get Element 关键字)。 获取元素的文本或属性(例如, 获取元素文本 和 获取元素属性 )。 直接验证文本,属性或整个元素(例如 元素文本应该 和 元素应该相等 )。 修改XML并保存它(例如, 设置元素文本 , 添加元素 和 保存XML )。
目录 解析XML 使用lxml 例 使用xpath查找元素 元素属性 处理XML名称空间 布尔参数 快捷键 关键词
解析XML
可以使用 Parse XML 关键字将XML解析为元素结构。它接受XML文件的路径和包含XML的字符串。关键字返回结构的根元素,然后包含其他元素作为其子元素及其子元素。将删除源XML中可能的注释和处理指令。
即使已定义架构,在解析期间也不验证XML。如何处理doctype元素,否则取决于使用的XML模块和平台。标准的ElementTree完全剥离了doctypes,但是当 使用lxml时, 它们会在保存XML时保留。使用IronPython时,根本不支持使用doctype解析XML。
Parse XML 返回的元素结构以及 Get Element source 等关键字返回的 元素 可以用作其他关键字的参数。除了已经解析的XML结构之外,其他关键字也接受XML文件的路径和包含XML的字符串,类似于 Parse XML 。请注意,修改XML的关键字不会将这些更改写回磁盘,即使源是作为文件的路径提供的。必须始终使用 Save XML 关键字显式 保存 更改。
当源作为文件的路径给出时,正斜杠字符( / )可以用作路径分隔符,而不管操作系统如何。在Windows上也可以使用反斜杠,但它需要通过加倍( \\ )来转义它所需的测试数据。使用内置变量也很 ${/} 自然。
使用lxml
默认情况下,此库使用Python的标准 ElementTree 模块来解析XML,但可以将其配置为在 导入 库时使用 lxml 模块。无论使用哪个模块进行解析,结果元素结构都具有相同的API。
使用lxml的主要好处是它支持比标准ElementTree更丰富的xpath语法,并允许使用 Evaluate Xpath 关键字。它还保留了保存XML的doctype和可能的名称空间前缀。
lxml支持是Robot Framework 2.8.5中的新增功能。

下面的简单示例演示了如何使用此库中的关键字以及 BuiltIn 和 Collections 库来解析XML并验证其内容。如何使用xpath表达式来查找元素以及返回的元素包含哪些属性,以及更多示例,在“ 使用xpath 和 元素属性 查找元素” 部分中进行讨论。
在此示例中,以及本文档中的许多其他示例中,请 ${XML} 参考以下示例XML文档。实际上, ${XML} 它可以是XML文件的路径,也可以包含XML本身。 <实例> <first id =“1”> text </ first> <second id =“2”> <子/> </秒> <第三> <child>更多文字</ child> <second id =“child”/> <子> <孙子/> </子> </第三> <HTML> <P> 带<b>粗体</ b>和<i>斜体</ i>的文字。 </ p> </ HTML> </示例>
$ {root} = 解析XML $ {} XML
应该是平等的 $ {} root.tag 例
$ {first} = 获取元素 $ {}根 第一
应该是平等的 $ {} first.text 文本
字典应该包含密钥 $ {} first.attrib ID
元素文本应该是 元素属性应该是 元素属性应该是 元素属性应该是
$ {}第一 $ {}第一 $ {}根 $ {} XML
文本 ID ID ID
1 1 1
的xpath =第一 的xpath =第一

请注意,在示例中,最后三行是等效的。在实践中使用哪一个取决于您需要获取或验证的其他元素。如果您只需要进行一次验证,仅使用最后一行就足够了。如果需要更多验证,只使用 Parse XML解析XML 一次会更有效。
使用xpath查找元素
ElementTree以及此库也支持使用xpath表达式查找元素。但是,ElementTree不支持完整的xpath语法,支持的内容取决于其版本。随Python 2.7一起发布的ElementTree 1.3支持比早期版本更丰富的语法。
支持的xpath语法如下所述, ElementTree文档 提供了更多详细信息。在示例中, ${XML} 引用与先前示例中相同的XML结构。
如果在 导入 库时启用了lxml支持,则支持整个 xpath 1.0标准 。这包括下面列出的所有内容,但也包括许多其他有用的结构。
标记名称
如果仅使用单个标记名称,则xpath将匹配具有该标记名称的所有直接子元素。
$ {elem} = 获取元素 $ {} XML 第三
应该是平等的 @ {children} = 应该是长度
$ {} elem.tag 获取元素 $ {}儿童
第三 $ {} ELEM 2
儿童

路径
通过将标记名称与正斜杠( / )组合来创建路径。例如, parent/child 匹配 child 元素下的所有元素 parent 。请注意,如果有多个 parent 元素都具有 child 元素,则 parent/child xpath将匹配所有这些 child 元素。
$ {elem} = 获取元素 $ {} XML 第二/儿童
应该是平等的 $ {elem} = 应该是平等的
$ {} elem.tag 获取元素 $ {} elem.tag
儿童 $ {} XML 孙子
第三个/孩子/孙子

通配符
* 可以在路径中使用星号()而不是标记名称来表示任何元素。

@ {children} = 应该是长度
获取元素 $ {}儿童
$ {} XML 3
*/儿童

当前元素
当前元素用点( . )表示。通常,当前元素是隐式的,不需要包含在xpath中。
父元素
另一个元素的父元素用两个点( .. )表示。请注意,无法引用当前元素的父元素。仅在ElementTree 1.3(即Python / Jython 2.7及更高版本)中支持此语法。

$ {elem} = 应该是平等的
获取元素 $ {} elem.tag
$ {} XML 第三
*/第二/..

搜索所有子元素
两个正斜杠( // )表示搜索所有子元素,而不仅仅是直接子元素。如果从当前元素开始搜索,则需要显式点。
@ {elements} = 获取元素 $ {} XML 。//第二
应该是长度 $ {b} = 应该是平等的
$ {}元素 获取元素 $ {} b.text
2 $ {} XML 胆大
HTML // B

谓词
谓词允许使用除标签名称之外的其他标准来选择元素,例如,属性或位置。它们使用语法在普通标记名称或路径之后指定 path[predicate] 。该路径可以具有上面解释的通配符和其他特殊语法。
ElementTree支持的谓词在下表中说明。请注意,通常只在ElementTree 1.3中支持谓词(即Python / Jython 2.7和更新版本)。
谓词 火柴 例
@attrib 具有属性的元素 attrib 。 第二[@id]
@属性=“值” 具有属性 attrib 值的元素 value 。 * [@ ID = “2”]
位置 标签
指定位置的元素。位置可以是整数(从1开始),表达式 last() 或相对表达式 last() - 1 。 带有子元素的元素 tag 。
第三/子[1] 第三/儿童[孙子]

谓词也可以堆叠起来 path[predicate1][predicate2] 。一个限制是可能的位置谓词必须始终是第一位的。
元素属性
返回元素的所有关键字(例如 Parse XML 和 Get Element )都返回ElementTree的 Element对象 。这些元素可以用作其他关键字的输入,但它们还包含几个可以使用扩展变量语法直接访问的有用属性。
下面解释了在测试数据中使用既有用又方便的属性。还可以访问其他属性(包括方法),但这通常在自定义库中比直接在测试数据中更好。
这些示例使用与 ${XML} 前面示例相同的结构。
标签
元素的标记。

$ {root} = 应该是平等的
解析XML $ {} root.tag
$ {} XML 例

文本
元素包含的文本或者 None 元素没有文本的Python 。请注意,文本 不 包含可能的子元素的文本 ,也不 包含子项之间或之间的文本。另请注意,在XML空格中很重要,因此文本还包含缩进和换行符。要获取可能子项的文本(可选择空格标准化),请使用“ 获取元素文本” 关键字。
$ {1st} = 获取元素 $ {} XML 第一
应该是平等的 $ {} 1st.text 文本
$ {2nd} = 获取元素 $ {} XML 第二/儿童
应该是平等的 $ {p} = 应该是平等的
$ {} 2nd.text 获取元素 $ {} p.text
$ {无} $ {} XML \ n $ {SPACE * 6}带$ {SPACE}的文字
HTML / P

尾巴
在下一个打开或关闭标记之前的元素之后的文本。Python None 如果元素没有尾部。与此类似 text ,还 tail 包含可能的缩进和换行符。

$ {b} = 应该是平等的
获取元素 $ {} b.tail
$ {} XML $ {SPACE}和$ {空白}
HTML / P / B

ATTRIB
包含元素属性的Python字典。
$ {2nd} = 获取元素 $ {} XML 第二
应该是平等的 $ {3rd} = 应该是空的
$ {2nd.attrib [ '身份证']} 获取元素 $ {} 3rd.attrib
2 $ {} XML
第三

处理XML名称空间
ElementTree和lxml通过将名称空间URI添加到所谓的Clark Notation中的标记名称来处理XML文档中可能的名称空间。这对于xpaths来说是不方便的,默认情况下,这个库会剥离这些名称空间并将它们移动到 xmlns 属性。通过将 keep_clark_notation 参数传递给 Parse XML 关键字可以避免这种情况。或者, Parse XML 支持使用 strip_namespaces 参数完全剥离命名空间信息。下面更详细地讨论不同方法的优缺点。
ElementTree如何处理名称空间
如果XML文档具有名称空间,ElementTree会将名称空间信息添加到 Clark Notation中的 标记名称(例如 {http://ns.uri}tag )并删除原始 xmlns 属性。使用默认命名空间和带前缀的命名空间都可以完成此操作。以下示例说明了它在实践中的工作原理,其中 ${NS} 变量包含此XML文档: <xsl:stylesheet xmlns:xsl =“http://www.w3.org/1999/XSL/Transform” 的xmlns = “http://www.w3.org/1999/xhtml”> <xsl:template match =“/”> <HTML> </ HTML> </ XSL:模板> </ XSL:样式>
$ {root} = 解析XML $ {} NS keep_clark_notation = YES
应该是平等的 元素应该存在 应该是空的
$ {} root.tag $ {}根 $ {} root.attrib
{http://www.w3.org/1999/XSL/Transform}stylesheet {http://www.w3.org/1999/XSL/Transform}template/{http://www.w3.org/1999/xhtml}html


正如您所看到的,在标记名称中包含名称空间URI会使xpath变得非常冗长和复杂。
如果保存XML,ElementTree会将名称空间信息移回 xmlns 属性。不幸的是它没有恢复原始前缀: <ns0:stylesheet xmlns:ns0 =“http://www.w3.org/1999/XSL/Transform”> <ns0:template match =“/”> <ns1:html xmlns:ns1 =“http://www.w3.org/1999/xhtml”> </ ns1:html> </ NS0:模板> </ NS0:样式>
结果输出在语义上与原始输出相同,但是这样的修改前缀可能仍然不可取。另请注意,实际输出略微取决于ElementTree版本。
默认命名空间处理
由于ElementTree处理命名空间的方式使xpath变得如此复杂,因此默认情况下,此库从标记名称中删除命名空间并将该信息移回 xmlns 属性。下面的示例显示了它在实践中的工作原理,其中 ${NS} 变量包含与上一示例中相同的XML文档。
$ {root} = 解析XML $ {} NS
应该是平等的 元素应该存在 元素属性应该是 元素属性应该是
$ {} root.tag $ {}根 $ {}根 $ {}根
样式表 模板/ HTML XMLNS XMLNS
http://www.w3.org/1999/XSL/Transform http://www.w3.org/1999/xhtml
的xpath =模板/ HTML

现在标签不包含名称空间信息,xpath再次简单。
此方法的一个小限制是名称空间前缀丢失。因此,在这种情况下,保存的输出与原始输出不完全相同: <stylesheet xmlns =“http://www.w3.org/1999/XSL/Transform”> <template match =“/”> <html xmlns =“http://www.w3.org/1999/xhtml”> </ html> </模板> </样式表>
此输出在语义上也与原始输出相同。如果原始XML只有默认名称空间,则输出看起来也相同。
使用lxml时的命名空间
此库在 使用lxml 和不使用时都处理名称空间。但是,与标准ElementTree相比,lxml在内部处理命名空间的方式存在差异。主要区别在于lxml存储有关名称空间前缀的信息,因此如果保存XML则会保留它们。另一个明显的区别是,如果父元素具有名称空间,则lxml包含使用 Get Element获取 的子元素中的名称空间信息。
完全剥离命名空间
由于命名空间通常会增加不必要的复杂性,因此 Parse XML 支持使用完全剥离它们 strip_namespaces=True 。启用此选项后,如果保存XML,则名称空间不会显示在任何位置,也不会包含在名称空间中。
属性名称空间
默认情况下,XML文档中的属性与它们所属的元素位于相同的名称空间中。通过使用前缀可以使用不同的命名空间,但这种情况非常罕见。
如果属性具有名称空间前缀,则ElementTree将使用与处理元素相同的方式将其替换为Clark Notation。因为从属性中删除命名空间可能会导致属性冲突,所以此库根本不处理属性命名空间。因此,无论如何处理命名空间,以下示例的工作方式都相同。

$ {root} = 元素属性应该是 元素属性应该是
解析XML $ {}根 $ {}根
<root id =“1”ns:id =“2”xmlns:ns =“http://my.ns”/> ID {HTTP://my.ns} ID
1 2

布尔参数
某些关键字接受以布尔值true或false处理的参数。如果这样的参数以字符串形式给出,则如果它是空字符串或不区分大小写,则被视为false false , none 或 no 。无论其值如何,其他字符串都被视为true,其他参数类型使用与 Python 相同的 规则 进行测试。
真实的例子:
解析XML $ {} XML keep_clark_notation =真 #字符串通常是正确的。
解析XML 解析XML 解析XML
$ {} XML $ {} XML $ {} XML
keep_clark_notation = YES keep_clark_notation = $ {TRUE} keep_clark_notation = $ {42}
#与上述相同。 #Pcthon True 是真的。 #0以外的数字为真。

错误的例子:
解析XML $ {} XML keep_clark_notation =假 #String false 为false。
解析XML 解析XML 解析XML
$ {} XML $ {} XML $ {} XML
keep_clark_notation =无 keep_clark_notation = $ {EMPTY} keep_clark_notation = $ {FALSE}
#字符串 no 也是false。 #Empty字符串为false。 #Python False 是假的。

在Robot Framework 2.9之前,所有非空字符串(包括 false 和 no )都被认为是真的。 none 在Robot Framework 3.0.3中考虑false是新的。
输入
参数 文档
use_lxml =假
导入库,启用了可选的lxml模式。
默认情况下,此库使用Python的标准 ElementTree 模块来解析XML。如果 use_lxml 参数被赋予真值(参见 布尔参数 ),则库将使用 lxml 模块。有关 lxml 提供的好处,请参阅 使用lxml 部分。
使用lxml需要在系统上安装lxml模块。如果启用了lxml模式但未安装模块,则此库将发出警告并恢复使用标准ElementTree。
对lxml的支持是Robot Framework 2.8.5中的新增功能。

快捷键
添加元素 · 清除元素 · copy元素 · 元素属性应该是 · 元素属性应该匹配 · 元素应该存在 · 元素应该不存在 · 元素应该没有属性 · 元素文字应该 · 元素文本应该匹配 · 元至字符串 · 元素应该相等 · 元素应该匹配 · 评估Xpath · 获取子元素 · 获取元素 · 获取元素属性 · 获取元素属性 · 获取元素计数 · 获取元素文本 · 获取元素 · 获取元素文本 · 日志元素 · 解析Xml · 删除元素 · 删除元素属性 · 删除元素属性 · 删除元素 · 删除元素属性 · 删除元素属性 · 保存Xml · 设置元素属性 · 设置元素标记 · 设置元素文本 · 设置元素属性 · 设置元素标签 · 设置元素文本
关键词
关键词 参数 文档
添加元素 source , element , index = None , xpath =。 将子元素添加到指定的元素。
要使用 source 和指定要添加新元素的元素 xpath 。它们与 Get Element 关键字具有完全相同的语义。返回生成的XML结构,如果 source 是已经解析的XML结构,则也会对其进行修改。
所述 element 添加可以被指定为到XML文件的路径或作为含有XML字符串,或者它可以是一个已经被解析XML元素。在添加元素之前复制元素,因此修改原始元素或添加元素对另一元素没有影响。默认情况下,该元素作为最后一个子元素添加,但可以使用自定义索引来更改位置。指数从零开始(0 =第一个位置,1 =第二个位置等),负数指的是结尾处的位置(-1 =倒数第二个位置,-2 =倒数第三个位置等)。
使用示例 ${XML} 结构的 示例 :
添加元素 $ {} XML <new id =“x”> <c1 /> </ new>
添加元素 添加元素 $ {new} = 要素应该是平等的
$ {} XML $ {} XML 获取元素 $ {}新
<C2 /> <C3 /> $ {} XML <new id =“x”> <c1 /> <c3 /> <c2 /> </ new>
XPath的=新 索引= 1 新
XPath的=新

使用“ 删除元素” 或“ 删除元素” 删除元素。
清除元素 source , xpath =。 , clear_tail =假 清除指定元素的内容。
要清除的元素使用 source 和指定 xpath 。它们与 Get Element 关键字具有完全相同的语义。返回生成的XML结构,如果 source 是已经解析的XML结构,则也会对其进行修改。
清除元素意味着删除其文本,属性和子元素。默认情况下不会删除元素的尾部文本,但可以通过给出 clear_tail 一个真值来更改它(请参阅 布尔参数 )。有关尾部的更多信息,请参阅 元素属性 部分。
使用示例 ${XML} 结构的 示例 :
清除元素 $ {} XML 的xpath =第一
$ {first} = 获取元素 $ {} XML 的xpath =第一
要素应该是平等的 $ {}第一 <第一/>
清除元素 元素文本应该是 清除元素 要素应该是平等的
$ {} XML $ {} XML $ {} XML $ {} XML
的xpath = HTML / P / B 带斜体的文字。 <示例/>
clear_tail = YES 的xpath = HTML / P
normalize_whitespace = YES

使用“ 删除元素” 删除整个元素。
复制元素 source , xpath =。 返回指定元素的副本。
要复制的元素使用 source 和指定 xpath 。它们与 Get Element 关键字具有完全相同的语义。
如果之后修改了副本或原始元素,则更改对另一个没有影响。
使用示例 ${XML} 结构的 示例 :
$ {elem} = 获取元素 $ {} XML 的xpath =第一
$ {copy1} = 复制元素 $ {} ELEM
$ {copy2} = 复制元素 $ {} XML 的xpath =第一
设置元素文本 $ {} XML 新文本 的xpath =第一
设置元素属性 $ {} COPY1 ID 新
要素应该是平等的 要素应该是平等的 要素应该是平等的
$ {} ELEM $ {} COPY1 $ {} COPY2
<first id =“1”>新文字</ first> <first id =“new”> text </ first> <first id =“1”> text </ first>


元素属性应该是 source , name , expected , xpath =。 , 消息=无 验证指定的属性是否为 expected 。
使用 source 和指定其属性已验证的元素 xpath 。它们与 Get Element 关键字具有完全相同的语义。
如果 name 元素的属性等于 expected 值,则关键字通过,否则失败。可以使用 message 参数覆盖默认错误消息。
为了测试元素没有某个属性,Python None (即变量 ${NONE} )可以用作期望值。更清洁的替代方案是使用 Element We Not Attribute 。
使用示例 ${XML} 结构的 示例 :

元素属性应该是 元素属性应该是
$ {} XML $ {} XML
ID ID
1 $ {无}
的xpath =第一

另请参见 元素属性应匹配 和 获取元素属性 。
元素属性应该匹配 来源 , 名称 , 模式 , xpath =。 , 消息=无 验证指定的属性是否匹配 expected 。
此关键字的作用与“ 元素属性应该是的” 完全相同,只是可以将期望值作为元素属性必须匹配的模式给出。
模式匹配与shell中的匹配文件类似,并且始终区分大小写。在模式中,'*'匹配任何内容和'?' 匹配任何单个字符。
使用示例 ${XML} 结构的 示例 :

元素属性应该匹配 元素属性应该匹配
$ {} XML $ {} XML
ID ID
? 光盘
的xpath =第一 的xpath =第三/第二

元素应该存在 source , xpath =。 , 消息=无 验证一个或多个元素是否与给定元素匹配 xpath 。
参数 source 和 xpath 具有同样的语义与 获取的元素 关键字。如果 xpath 匹配中的一个或多个元素,则关键字传递 source 。可以使用 message 参数覆盖默认错误消息。
另请参见 元素不应存在 以及此关键字在内部使用的 获取元素计数 。
元素不应该存在 source , xpath =。 , 消息=无 验证没有元素与给定的匹配 xpath 。
参数 source 和 xpath 具有同样的语义与 获取的元素 关键字。如果 xpath 匹配中的任何元素,则关键字将失败 source 。可以使用 message 参数覆盖默认错误消息。
另请参见 元素应存在 以及此关键字在内部使用的 获取元素计数 。
元素不应该有属性 source , name , xpath =。 , 消息=无 验证指定的元素是否没有属性 name 。
使用 source 和指定其属性已验证的元素 xpath 。它们与 Get Element 关键字具有完全相同的语义。
如果指定的元素具有属性,则关键字将失败 name 。可以使用 message 参数覆盖默认错误消息。
使用示例 ${XML} 结构的 示例 :

元素不应该有属性 元素不应该有属性
$ {} XML $ {} XML
ID XXX
的xpath =第一

另请参阅 获取元素属性 , 获取元素属性 , 元素文本应该 和 元素文本应该匹配 。
元素文本应该是 source , expected , xpath =。 , normalize_whitespace = False , message = None 验证指定元素的文本是否为 expected 。
验证文本的元素使用 source 和指定 xpath 。它们与 Get Element 关键字具有完全相同的语义。
要使用与 Get Element Text 相同的逻辑从指定的元素 获取 要验证的 文本 。这包括使用该 normalize_whitespace 选项的可选空白规范化。
如果元素的文本等于 expected 值,则关键字通过,否则失败。可以使用 message 参数覆盖默认错误消息。使用 元素文本应匹配 以根据模式而不是精确值验证文本。
使用示例 ${XML} 结构的 示例 :
元素文本应该是 $ {} XML 文本 的xpath =第一
元素文本应该是 $ {paragraph} = 元素文本应该是
$ {} XML 获取元素 $ {}段
$ {EMPTY} $ {} XML 带粗体和斜体的文字。
的xpath =第二/子 的xpath = HTML / P normalize_whitespace = YES

元素文本应该匹配 source , pattern , xpath =。 , normalize_whitespace = False , message = None 验证指定元素的文本是否匹配 expected 。
此关键字的工作方式与“ 元素文本应该” 完全相同,只是可以将期望值作为元素文本必须匹配的模式给出。
模式匹配与shell中的匹配文件类似,并且始终区分大小写。在模式中,'*'匹配任何内容和'?' 匹配任何单个字符。
使用示例 ${XML} 结构的 示例 :

元素文本应该匹配 $ {paragraph} = 元素文本应该匹配
$ {} XML 获取元素 $ {}段
T 450 $ {} XML 带*和*的文字。
的xpath =第一 的xpath = HTML / P normalize_whitespace = YES

元素到字符串 source , xpath =。 , encoding =无 返回指定元素的字符串表示形式。
要转换为字符串的元素是使用 source 和指定的 xpath 。它们与 Get Element 关键字具有完全相同的语义。
默认情况下,字符串以Unicode格式返回。如果 encoding 赋值为任何值,则字符串将以指定编码中的字节形式返回。结果字符串永远不会包含XML声明。
另请参见 日志元素 和 保存XML 。
要素应该是平等的 source , expected , exclude_children = False , normalize_whitespace = False 验证给定 source 元素是否等于 expected 。
二者 source 并 expected 可以作为一个XML文件的路径,作为含XML字符串,或者作为已经被解析XML元素结构。有关解析XML的更多信息,请参阅 简介 。
如果 source 元素和 expected 元素相等,则关键字通过。这包括测试元素的标签名称,文本和属性。默认情况下,也会以相同的方式验证子元素,但可以通过设置 exclude_children 为true值来禁用它(请参阅 布尔参数 )。
验证给定元素内的所有文本,但不包括它们之外的可能文本。默认情况下,文本必须完全匹配,但设置 normalize_whitespace 为true值会使文本验证独立于换行符,制表符和空格量。有关处理文本的更多详细信息,请参阅 获取元素文本 约元素的关键字和讨论 文本 和 尾部 的属性 介绍 。
使用示例 ${XML} 结构的 示例 :
$ {first} = 获取元素 $ {} XML 第一
要素应该是平等的 $ {p} = 要素应该是平等的 要素应该是平等的
$ {}第一 获取元素 $ {P} $ {P}
<first id =“1”> text </ first> $ {} XML <p>带<b>粗体</ b>和<i>斜体</ i>的文字。</ p> <p>带</ p>的文字
HTML / P normalize_whitespace = YES 排除
正常化

最后一个示例可能看起来有点奇怪,因为该 <p> 元素只有文本 Text with 。原因是内部文本的其余部分 <p> 实际上属于子元素。这包括 . 最后一个元素的 尾部 文本 <i> 。
另请参见 元素应匹配 。
元素应该匹配 source , expected , exclude_children = False , normalize_whitespace = False 验证给定 source 元素是否匹配 expected 。
此关键字的作用与 Elements Should Be Equal 完全相同,只是期望值中的文本和属性值可以作为模式给出。
模式匹配与shell中的匹配文件类似,并且始终区分大小写。在模式中,'*'匹配任何内容和'?' 匹配任何单个字符。
使用示例 ${XML} 结构的 示例 :

$ {first} = 元素应该匹配
获取元素 $ {}第一
$ {} XML <first id =“?”> * </ first>
第一

有关更多示例,请参阅 元素应该相等 。
评估Xpath 来源 , 表达 , 上下文=。 计算给定的xpath表达式并返回结果。
使用 source 和 context arguments 指定执行表达式的上下文的元素。它们与 Get Element 关键字具有完全相同的语义 source 和 xpath 参数。
要评估的xpath表达式作为 expression 参数给出。评估结果按原样返回。
使用示例 ${XML} 结构的 示例 :
$ {count} = 评估Xpath $ {} XML 计数(第三/ *)
应该是平等的 $ {}计数 $ {3}
$ {text} = 应该是平等的 $ {bold} = 应该是平等的
评估Xpath $ {}文 评估Xpath $ {}大胆
$ {} XML 儿童 $ {} XML $ {TRUE}
串(后裔::第二[最后一个()] / @ id)的 boolean(preceding-sibling :: * [1] ='bold')
上下文= HTML / P / I

仅当在 导入 库时使用lxml模式时,此关键字才有效。机器人框架2.8.5中的新功能。
获取子元素 source , xpath =。 以列表形式返回指定元素的子元素。
要返回的子元素使用 source 和指定的元素 xpath 。它们与 Get Element 关键字具有完全相同的语义。
返回指定元素的所有直接子元素。如果元素没有子元素,则返回空列表。
使用示例 ${XML} 结构的 示例 :
$ {children} = 获取子元素 $ {} XML
应该是长度 $ {children} = 应该是空的
$ {}儿童 获取子元素 $ {}儿童
4 $ {} XML
的xpath =第一

获取元素 source , xpath =。 返回 source 匹配的元素 xpath 。
它 source 可以是XML文件的路径,包含XML的字符串或已解析的XML元素。该 xpath 指定哪个元素找到。有关可能的源和支持的xpath语法的更多详细信息,请参阅 简介 。
如果多于或少于一个元素匹配,则关键字失败 xpath 。如果要返回所有匹配的元素,请使用“ 获取元素” 。
使用示例 ${XML} 结构的 示例 :

$ {element} = $ {child} =
获取元素 获取元素
$ {} XML $ {}元素
第二 儿童

当需要整个结构时,建议使用 Parse XML 来解析XML。如果需要配置XML命名空间的处理方式,则必须使用它。
许多其他关键字在内部使用此关键字,并且通常将修改XML的关键字记录到两者以修改给定的源并返回它。如果源是以字符串形式给出,则修改源不适用。然后返回基于字符串解析然后修改的XML结构。
获取元素属性 source , name , xpath =。 , 默认=无 返回指定元素的命名属性。
要使用 source 和指定要返回其属性的元素 xpath 。它们与 Get Element 关键字具有完全相同的语义。
name 返回指定元素的属性值。如果元素没有这样的元素, default 则返回该值。
使用示例 ${XML} 结构的 示例 :

$ {attribute} = 应该是平等的 $ {attribute} = 应该是平等的
获取元素属性 $ {}属性 获取元素属性 $ {}属性
$ {} XML 1 $ {} XML 值
ID XX
的xpath =第一 的xpath =第一
默认=值

另请参阅 获取元素属性 , 元素属性应该是 , 元素属性应匹配 且 元素不应具有属性 。
获取元素属性 source , xpath =。 返回指定元素的所有属性。
要返回的元素使用 source 和指定 xpath 。它们与 Get Element 关键字具有完全相同的语义。
属性作为Python字典返回。它是原始属性的副本,因此修改它对XML结构没有影响。
使用示例 ${XML} 结构的 示例 :
$ {attributes} = 获取元素属性 $ {} XML 第一
字典应该包含密钥 $ {attributes} = 应该是空的
$ {}属性 获取元素属性 $ {}属性
ID $ {} XML
第三

使用“ 获取元素属性” 获取单个属性的值。
获取元素计数 source , xpath =。 返回并记录给定 xpath 匹配的元素数。
参数 source 与此关键字内部使用的 Get Elements 关键字 xpath 具有完全相同的语义。
另请参见 元素应存在 且 元素不应存在 。
获取元素文本 source , xpath =。 , normalize_whitespace = False 返回元素的所有文本,可能是空格规范化的。
要返回的文本的元素是使用 source 和指定的 xpath 。它们与 Get Element 关键字具有完全相同的语义。
此关键字返回指定元素的所有文本,包括其子元素和孙子元素包含的所有文本。如果元素没有文本,则返回空字符串。因此,返回的文本并不总是与元素的 text 属性相同。
默认情况下,元素内的所有空格(包括换行符和缩进)都按原样返回。如果 normalize_whitespace 给出了一个真值(参见 布尔参数 ),则删除前导和尾随空格,将换行符和制表符转换为空格,将多个空格折叠为一个空格。这在处理HTML数据时特别有用。
使用示例 ${XML} 结构的 示例 :
$ {text} = 获取元素文本 $ {} XML 第一
应该是平等的 $ {}文 文本
$ {text} = 获取元素文本 $ {} XML 第二/儿童
应该是空的 $ {}文
$ {paragraph} = $ {text} = 应该是平等的
获取元素 获取元素文本 $ {}文
$ {} XML $ {}段 带粗体和斜体的文字。
HTML / P normalize_whitespace = YES

另请参阅 获取元素文本 , 元素文本应该 和 元素文本应该匹配 。
获取元素 source , xpath 返回 source 匹配的元素列表 xpath 。
它 source 可以是XML文件的路径,包含XML的字符串或已解析的XML元素。该 xpath 指定哪个元素找到。有关详细信息,请参阅 简介 。
匹配的元素将 xpath 作为列表返回。如果没有元素匹配,则返回空列表。如果您想获得一场比赛,请使用 获取元素 。
使用示例 ${XML} 结构的 示例 :
$ {children} = 获取元素 $ {} XML 第三/儿童
应该是长度 $ {children} = 应该是空的
$ {}儿童 获取元素 $ {}儿童
2 $ {} XML
第一个孩子

获取元素文本 source , xpath , normalize_whitespace = False 返回 xpath 作为列表匹配的所有元素的文本。
要返回的文本使用 source 和指定的元素 xpath 。它们与 Get Elements 关键字具有完全相同的语义。
使用与 Get Element Text 相同的逻辑返回匹配元素的 文本 。这包括使用该 normalize_whitespace 选项的可选空白规范化。
使用示例 ${XML} 结构的 示例 :
@ {texts} = 获取元素文本 $ {} XML 第三/儿童
应该是长度 应该是平等的 应该是平等的
$ {}文本 @ {文本} [0] @ {文本} [1]
2 更多文字 $ {EMPTY}


日志元素 source , level = INFO , xpath =。 记录指定元素的字符串表示形式。
使用 source 和指定的元素 xpath 首先在内部使用 Element To String 关键字转换为字符串。然后使用给定的字符串记录生成的字符串 level 。
还会返回记录的字符串。
解析Xml source , keep_clark_notation = False , strip_namespaces = False 将给定的XML文件或字符串解析为元素结构。
的 source 可以是XML文件或含XML字符串的路径。在这两种情况下,XML都被解析为ElementTree 元素结构, 并返回根元素。将删除源XML中可能的注释和处理指令。
正如在 处理XML名称空间 部分中所讨论的那样,默认情况下,此关键字会删除ElementTree添加到标记名称并将其移动到 xmlns 属性中的名称空间信息。这通常可以显着简化处理带有名称空间的XML文档。如果您不希望这种情况发生,或者想要避免在XML没有名称空间时通过元素结构的小开销,则可以通过为 keep_clark_notation 参数赋予true值来禁用此功能(请参阅 布尔参数 )。
如果要完全删除命名空间信息,以便即使保存XML也不包含它,您可以为 strip_namespaces 参数赋予真值。此功能是Robot Framework 3.0.2中的新功能。
例子:

$ {root} = $ {xml} = $ {xml} =
解析XML 解析XML 解析XML
<根> <子/> </根> $ {} CURDIR /test.xml $ {} CURDIR /test.xml
keep_clark_notation =真 strip_namespaces =真

如果要获取某个元素而不是整个结构,请使用“ 获取元素” 关键字。有关更多详细信息和示例,请参阅 解析XML 部分。
删除元素 source , xpath = , remove_tail = False xpath 从 source 结构中删除匹配的元素。
使用与 Get Element 关键字相同的语义 source 指定要从中删除的 元素 。返回生成的XML结构,如果是已经解析的XML结构,则也会对其进行修改。 xpath source
如果关键字 xpath 与一个元素不匹配,则关键字失败。使用“ 删除元素” 删除所有匹配的元素。
默认情况下不会删除元素的尾部文本,但可以通过给出 remove_tail 一个真值来更改它(请参阅 布尔参数 )。有关 尾部的 更多信息,请参阅 元素属性 部分。
使用示例 ${XML} 结构的 示例 :

删除元素 元素不应该存在 删除元素 元素文本应该是
$ {} XML $ {} XML $ {} XML $ {} XML
的xpath =第二 的xpath =第二 的xpath = HTML / P / B 带斜体的文字。
remove_tail = YES 的xpath = HTML / P
normalize_whitespace = YES

删除元素属性 source , name , xpath =。 name 从指定的元素中删除属性。
要使用 source 和指定要删除其属性的元素 xpath 。它们与 Get Element 关键字具有完全相同的语义。返回生成的XML结构,如果 source 是已经解析的XML结构,则也会对其进行修改。
删除不存在的属性并非失败。使用“ 删除元素属性” 删除所有属性,使用“ 设置元素属性” 设置它们。
使用示例 ${XML} 结构的 示例 :

删除元素属性 元素不应该有属性
$ {} XML $ {} XML
ID ID
的xpath =第一 的xpath =第一

只能从单个元素中删除属性。使用“ 删除元素属性” 可在一次调用中删除多个元素的属性。
删除元素属性 source , xpath =。 从指定元素中删除所有属性。
要使用 source 和指定要删除的属性的元素 xpath 。它们与 Get Element 关键字具有完全相同的语义。返回生成的XML结构,如果 source 是已经解析的XML结构,则也会对其进行修改。
使用“ 删除元素属性” 删除单个属性,使用“ 设置元素属性” 设置它们。
使用示例 ${XML} 结构的 示例 :

删除元素属性 元素不应该有属性
$ {} XML $ {} XML
的xpath =第一 ID
的xpath =第一

只能从单个元素中删除属性。使用“ 删除元素属性” 可在一次调用中删除多个元素的所有属性。
删除元素 source , xpath = , remove_tail = False xpath 从 source 结构中删除所有匹配的元素。
要使用与 Get Elements 关键字相同的语义 source 指定要从中删除的 元素 。返回生成的XML结构,如果是已经解析的XML结构,则也会对其进行修改。 xpath source
如果 xpath 匹配没有元素,那不是失败。使用“ 删除元素” 可以删除一个元素。
默认情况下不会删除元素的尾部文本,但可以使用 remove_tail 与 删除元素 类似的参数来更改元素的尾部文本。
使用示例 ${XML} 结构的 示例 :
删除元素 $ {} XML XPath的= * /儿童
元素不应该存在 元素不应该存在
$ {} XML $ {} XML
的xpath =第二/子 XPath的=第三/儿童

删除元素属性 source , name , xpath =。 name 从指定的元素中删除属性。
与 删除元素属性 类似,但删除与给定匹配的所有元素的属性 xpath 。
Robot Framework 2.8.6中的新功能。
删除元素属性 source , xpath =。 从指定元素中删除所有属性。
与 删除元素属性 类似,但删除与给定匹配的所有元素的所有属性 xpath 。
Robot Framework 2.8.6中的新功能。
保存Xml 源 , 路径 , 编码= UTF-8 将给定元素保存到指定文件。
source 使用与 Get Element 关键字相同的语义指定要保存的 元素 。
保存元素的文件用 path 以及要使用的编码表示 encoding 。生成的文件始终包含XML声明。
生成的XML文件可能与原始文件不完全相同: 注释和处理指令始终被删除。 只有在 使用lxml 时才会保留可能的doctype和名称空间前缀。 其他细微差别可能取决于ElementTree或lxml版本。
如果您只需要元素的字符串表示,请使用 Element To String 。
设置元素属性 来源 , 名称 , 价值 , xpath =。 name 将指定元素的属性设置为 value 。
要使用 source 和指定要设置的属性的元素 xpath 。它们与 Get Element 关键字具有完全相同的语义。返回生成的XML结构,如果 source 是已经解析的XML结构,则也会对其进行修改。
既可以设置新属性又可以覆盖现有属性。使用“ 删除元素属性” 或“ 删除元素属性 ”将其删除。
使用示例 ${XML} 结构的 示例 :

设置元素属性 元素属性应该是 设置元素属性 元素属性应该是
$ {} XML $ {} XML $ {} XML $ {} XML
ATTR ATTR ID ID
值 值 新 新
的xpath =第一 的xpath =第一

只能设置单个元素的属性。使用“ 设置元素属性” 在一次调用中设置多个元素的属性。
设置元素标记 source , tag , xpath =。 设置指定元素的标记。
要使用 source 和指定要设置的标记的元素 xpath 。它们与 Get Element 关键字具有完全相同的语义。返回生成的XML结构,如果 source 是已经解析的XML结构,则也会对其进行修改。
使用示例 ${XML} 结构的 示例 :
设置元素标记 $ {} XML newTag
应该是平等的 $ {} XML.tag newTag
设置元素标记 元素应该存在 元素不应该存在
$ {} XML $ {} XML $ {} XML
XXX 第二/ XXX 第二/儿童
的xpath =第二/子

只能设置单个元素的标记。使用“ 设置元素标记” 在一次调用中设置多个元素的标记。
设置元素文本 source , text = None , tail = None , xpath =。 设置指定元素的文本和/或尾部文本。
要使用 source 和指定要设置的文本的元素 xpath 。它们与 Get Element 关键字具有完全相同的语义。返回生成的XML结构,如果 source 是已经解析的XML结构,则也会对其进行修改。
仅当给出新值 text 和/或 tail 值时,才会更改元素的文本和尾部文本。有关 文本 和 尾部的 更多信息,请参阅 元素属性 部分。
使用示例 ${XML} 结构的 示例 :
设置元素文本 $ {} XML 新文本 的xpath =第一
元素文本应该是 $ {} XML 新文本 的xpath =第一
设置元素文本 元素文本应该是 设置元素文本 元素文本应该是
$ {} XML $ {} XML $ {} XML $ {} XML
尾=& 带粗体和斜体的文字。 倾斜 文字大胆倾斜!
的xpath = HTML / P / B 的xpath = HTML / P ! 的xpath = HTML / P
normalize_whitespace = YES 的xpath = HTML / P / I normalize_whitespace = YES

只能设置单个元素的文本/尾部。使用“ 设置元素文本” 在一次调用中设置多个元素的文本/尾部。
设置元素属性 来源 , 名称 , 价值 , xpath =。 name 将指定元素的属性设置为 value 。
与 Set Element Attribute 类似,但设置与给定元素匹配的所有元素的属性 xpath 。
Robot Framework 2.8.6中的新功能。
设置元素标记 设置元素文本
source , tag , xpath =。 source , text = None , tail = None , xpath =。
设置指定元素的标记。
与 Set Element Tag 类似,但设置与给定匹配的所有元素的标记 xpath 。
Robot Framework 2.8.6中的新功能。 设置指定元素的文本和/或尾部文本。
与 Set Element Text 类似,但设置与给定元素匹配的所有元素的文本或尾部 xpath 。
Robot Framework 2.8.6中的新功能。

共有37个关键字。
由 Libdoc 于2018-04-25 23:41:29 生成。

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行