Library version: | 3.0.4 |
---|---|
Library scope: | global |
Named arguments: | supported |
Robot Framework test library for verifying and modifying XML documents.html
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.python
The library has the following main usages:shell
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.express
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.app
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.框架
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.less
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.ide
The lxml support is new in Robot Framework 2.8.5.測試
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 | ${first} | text | ||
Element Attribute Should Be | ${first} | id | 1 | |
Element Attribute Should Be | ${root} | id | 1 | xpath=first |
Element Attribute Should Be | ${XML} | id | 1 | 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.
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.
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 | ${elem.tag} | third | |
@{children} = | Get Elements | ${elem} | child |
Length Should Be | ${children} | 2 |
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.tag} | child | |
${elem} = | Get Element | ${XML} | third/child/grandchild |
Should Be Equal | ${elem.tag} | grandchild |
An asterisk (*
) can be used in paths instead of a tag name to denote any element.
@{children} = | Get Elements | ${XML} | */child |
Length Should Be | ${children} | 3 |
The current element is denoted with a dot (.
). Normally the current element is implicit and does not need to be included in the xpath.
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} = | Get Element | ${XML} | */second/.. |
Should Be Equal | ${elem.tag} | third |
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 | ${elements} | 2 | |
${b} = | Get Element | ${XML} | html//b |
Should Be Equal | ${b.text} | bold |
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 | Elements at the specified position. Position can be an integer (starting from 1), expression last() , or relative expression like last() - 1 . |
third/child[1] |
tag | Elements with a child element named tag . |
third/child[grandchild] |
Predicates can also be stacked like path[predicate1][predicate2]
. A limitation is that possible position predicate must always be first.
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.
The tag of the element.
${root} = | Parse XML | ${XML} |
Should Be Equal | ${root.tag} | example |
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 | ${2nd.text} | ${NONE} | |
${p} = | Get Element | ${XML} | html/p |
Should Be Equal | ${p.text} | \n${SPACE*6}Text with${SPACE} |
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} = | Get Element | ${XML} | html/p/b |
Should Be Equal | ${b.tail} | ${SPACE}and${SPACE} |
A Python dictionary containing attributes of the element.
${2nd} = | Get Element | ${XML} | second |
Should Be Equal | ${2nd.attrib['id']} | 2 | |
${3rd} = | Get Element | ${XML} | third |
Should Be Empty | ${3rd.attrib} |
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.
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 | ${root.tag} | {http://www.w3.org/1999/XSL/Transform}stylesheet | |
Element Should Exist | ${root} | {http://www.w3.org/1999/XSL/Transform}template/{http://www.w3.org/1999/xhtml}html | |
Should Be Empty | ${root.attrib} |
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.
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 | ${root.tag} | stylesheet | ||
Element Should Exist | ${root} | template/html | ||
Element Attribute Should Be | ${root} | xmlns | http://www.w3.org/1999/XSL/Transform | |
Element Attribute Should Be | ${root} | xmlns | 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.
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.
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.
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} = | Parse XML | <root id="1" ns:id="2" xmlns:ns="http://my.ns"/> | |
Element Attribute Should Be | ${root} | id | 1 |
Element Attribute Should Be | ${root} | {http://my.ns}id | 2 |
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 | ${XML} | keep_clark_notation=yes | # Same as the above. |
Parse XML | ${XML} | keep_clark_notation=${TRUE} | # Python True is true. |
Parse XML | ${XML} | keep_clark_notation=${42} | # Numbers other than 0 are true. |
False examples:
Parse XML | ${XML} | keep_clark_notation=False | # String false is false. |
Parse XML | ${XML} | keep_clark_notation=no | # Also string no is false. |
Parse XML | ${XML} | keep_clark_notation=${EMPTY} | # Empty string is false. |
Parse XML | ${XML} | keep_clark_notation=${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.
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 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. |
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
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 The Examples using
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 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 Examples using
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 If the copy or the original element is modified afterwards, the changes have no effect on the other. Examples using
|
|||||||||||||||||||||||||||||||||||
Element Attribute Should Be | source, name, expected,xpath=., message=None | Verifies that the specified attribute is The element whose attribute is verified is specified using The keyword passes if the attribute To test that the element does not have a certain attribute, Python Examples using
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 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
|
|||||||||||||||||||||||||||||||||||
Element Should Exist | source, xpath=.,message=None | Verifies that one or more element match the given Arguments 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 Arguments 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 The element whose attribute is verified is specified using The keyword fails if the specified element has attribute Examples using
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 The element whose text is verified is specified using 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 The keyword passes if the text of the element is equal to the Examples using
|
|||||||||||||||||||||||||||||||||||
Element Text Should Match | source, pattern, xpath=.,normalize_whitespace=False,message=None | Verifies that the text of the specified element matches 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
|
|||||||||||||||||||||||||||||||||||
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 By default the string is returned as Unicode. If See also Log Element and Save XML. |
|||||||||||||||||||||||||||||||||||
Elements Should Be Equal | source, expected,exclude_children=False,normalize_whitespace=False | Verifies that the given Both The keyword passes if the All texts inside the given elements are verified, but possible text outside them is not. By default texts must match exactly, but setting Examples using
The last example may look a bit strange because the See also Elements Should Match. |
|||||||||||||||||||||||||||||||||||
Elements Should Match | source, expected,exclude_children=False,normalize_whitespace=False | Verifies that the given 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
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 The xpath expression to evaluate is given as Examples using
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 All the direct child elements of the specified element are returned. If the element has no children, an empty list is returned. Examples using
|
|||||||||||||||||||||||||||||||||||
Get Element | source, xpath=. | Returns an element in the The The keyword fails if more, or less, than one element matches the Examples using
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 The value of the attribute Examples using
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 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
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 Arguments 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 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 Examples using
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 The Elements matching the Examples using
|
|||||||||||||||||||||||||||||||||||
Get Elements Texts | source, xpath,normalize_whitespace=False | Returns text of all elements matching The elements whose text to return is specified using The text of the matched elements is returned using the same logic as with Get Element Text. This includes optional whitespace normalization using the Examples using
|
|||||||||||||||||||||||||||||||||||
Log Element | source, level=INFO, xpath=. | Logs the string representation of the specified element. The element specified with 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 As discussed in Handling XML namespaces section, this keyword, by default, removes namespace information ElementTree has added to tag names and moves it into 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 Examples:
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 The element to remove from the The keyword fails if Element's tail text is not removed by default, but that can be changed by giving Examples using
|
|||||||||||||||||||||||||||||||||||
Remove Element Attribute | source, name, xpath=. | Removes attribute The element whose attribute to remove is specified using 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
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 Use Remove Element Attribute to remove a single attribute and Set Element Attribute to set them. Examples using
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 The elements to remove from the It is not a failure if Element's tail text is not removed by default, but that can be changed by using Examples using
|
|||||||||||||||||||||||||||||||||||
Remove Elements Attribute | source, name, xpath=. | Removes attribute Like Remove Element Attribute but removes the attribute of all elements matching the given 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 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 The file where the element is saved is denoted with The resulting XML file may not be exactly the same as the original:
Use Element To String if you just need a string representation of the element. |
|||||||||||||||||||||||||||||||||||
Set Element Attribute | source, name, value,xpath=. | Sets attribute The element whose attribute to set is specified using 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
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 Examples using
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 Element's text and tail text are changed only if new Examples using
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 Like Set Element Attribute but sets the attribute of all elements matching the given New in Robot Framework 2.8.6. |
|||||||||||||||||||||||||||||||||||
Set Elements Tag | source, tag, xpath=. | Sets the tag of the specified elements. Like Set Element Tag but sets the tag of all elements matching the given New in Robot Framework 2.8.6. |
|||||||||||||||||||||||||||||||||||
Set Elements Text | source, text=None,tail=None, xpath=. | 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 New in Robot Framework 2.8.6. |
Altogether 37 keywords.
Generated by Libdoc on 2018-04-25 23:41:29.
圖書館版本: | 3.0.4 |
---|---|
圖書館範圍: | 全球 |
命名參數: | 支持的 |
Robot Framework測試庫,用於驗證和修改XML文檔。
顧名思義,XML是用於驗證XML文件內容的測試庫。實際上,它是Python的ElementTree XML API之上的一個很是薄的包裝器。
該庫具備如下主要用途:
可使用Parse XML關鍵字將XML解析爲元素結構。它接受XML文件的路徑和包含XML的字符串。關鍵字返回結構的根元素,而後包含其餘元素做爲其子元素及其子元素。將刪除源XML中可能的註釋和處理指令。
即便已定義架構,在解析期間也不驗證XML。如何處理doctype元素,不然取決於使用的XML模塊和平臺。標準的ElementTree徹底剝離了doctypes,可是當使用lxml時,它們會在保存XML時保留。使用IronPython時,根本不支持使用doctype解析XML。
Parse XML返回的元素結構以及Get Elementsource
等關鍵字返回的元素能夠用做其餘關鍵字的參數。除了已經解析的XML結構以外,其餘關鍵字也接受XML文件的路徑和包含XML的字符串,相似於Parse XML。請注意,修改XML的關鍵字不會將這些更改寫回磁盤,即便源是做爲文件的路徑提供的。必須始終使用Save XML關鍵字顯式保存更改。
當源做爲文件的路徑給出時,正斜槓字符(/
)能夠用做路徑分隔符,而無論操做系統如何。在Windows上也可使用反斜槓,但它須要經過加倍(\\
)來轉義它所需的測試數據。使用內置變量也很${/}
天然。
默認狀況下,此庫使用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 | ||
元素文本應該是 | $ {}第一 | 文本 | ||
元素屬性應該是 | $ {}第一 | ID | 1 | |
元素屬性應該是 | $ {}根 | ID | 1 | 的xpath =第一 |
元素屬性應該是 | $ {} XML | ID | 1 | 的xpath =第一 |
請注意,在示例中,最後三行是等效的。在實踐中使用哪個取決於您須要獲取或驗證的其餘元素。若是您只須要進行一次驗證,僅使用最後一行就足夠了。若是須要更多驗證,只使用Parse XML解析XML一次會更有效。
ElementTree以及此庫也支持使用xpath表達式查找元素。可是,ElementTree不支持完整的xpath語法,支持的內容取決於其版本。隨Python 2.7一塊兒發佈的ElementTree 1.3支持比早期版本更豐富的語法。
支持的xpath語法以下所述,ElementTree文檔提供了更多詳細信息。在示例中,${XML}
引用與先前示例中相同的XML結構。
若是在導入庫時啓用了lxml支持,則支持整個xpath 1.0標準。這包括下面列出的全部內容,但也包括許多其餘有用的結構。
若是僅使用單個標記名稱,則xpath將匹配具備該標記名稱的全部直接子元素。
$ {elem} = | 獲取元素 | $ {} XML | 第三 |
應該是平等的 | $ {} elem.tag | 第三 | |
@ {children} = | 獲取元素 | $ {} ELEM | 兒童 |
應該是長度 | $ {}兒童 | 2 |
經過將標記名稱與正斜槓(/
)組合來建立路徑。例如,parent/child
匹配child
元素下的全部元素parent
。請注意,若是有多個parent
元素都具備child
元素,則parent/child
xpath將匹配全部這些child
元素。
$ {elem} = | 獲取元素 | $ {} XML | 第二/兒童 |
應該是平等的 | $ {} elem.tag | 兒童 | |
$ {elem} = | 獲取元素 | $ {} XML | 第三個/孩子/孫子 |
應該是平等的 | $ {} elem.tag | 孫子 |
*
能夠在路徑中使用星號()而不是標記名稱來表示任何元素。
@ {children} = | 獲取元素 | $ {} XML | */兒童 |
應該是長度 | $ {}兒童 | 3 |
當前元素用點(.
)表示。一般,當前元素是隱式的,不須要包含在xpath中。
另外一個元素的父元素用兩個點(..
)表示。請注意,沒法引用當前元素的父元素。僅在ElementTree 1.3(即Python / Jython 2.7及更高版本)中支持此語法。
$ {elem} = | 獲取元素 | $ {} XML | */第二/.. |
應該是平等的 | $ {} elem.tag | 第三 |
兩個正斜槓(//
)表示搜索全部子元素,而不只僅是直接子元素。若是從當前元素開始搜索,則須要顯式點。
@ {elements} = | 獲取元素 | $ {} XML | 。//第二 |
應該是長度 | $ {}元素 | 2 | |
$ {b} = | 獲取元素 | $ {} XML | HTML // B |
應該是平等的 | $ {} b.text | 膽大 |
謂詞容許使用除標籤名稱以外的其餘標準來選擇元素,例如,屬性或位置。它們使用語法在普通標記名稱或路徑以後指定path[predicate]
。該路徑能夠具備上面解釋的通配符和其餘特殊語法。
ElementTree支持的謂詞在下表中說明。請注意,一般只在ElementTree 1.3中支持謂詞(即Python / Jython 2.7和更新版本)。
謂詞 | 火柴 | 例 |
---|---|---|
@attrib | 具備屬性的元素attrib 。 |
第二[@id] |
@屬性=「值」 | 具備屬性attrib 值的元素value 。 |
* [@ ID = 「2」] |
位置 | 指定位置的元素。位置能夠是整數(從1開始),表達式last() 或相對錶達式last() - 1 。 |
第三/子[1] |
標籤 | 帶有子元素的元素tag 。 |
第三/兒童[孫子] |
謂詞也能夠堆疊起來path[predicate1][predicate2]
。一個限制是可能的位置謂詞必須始終是第一位的。
返回元素的全部關鍵字(例如Parse XML和Get Element)都返回ElementTree的Element對象。這些元素能夠用做其餘關鍵字的輸入,但它們還包含幾個可使用擴展變量語法直接訪問的有用屬性。
下面解釋了在測試數據中使用既有用又方便的屬性。還能夠訪問其餘屬性(包括方法),但這一般在自定義庫中比直接在測試數據中更好。
這些示例使用與${XML}
前面示例相同的結構。
元素的標記。
$ {root} = | 解析XML | $ {} XML |
應該是平等的 | $ {} root.tag | 例 |
元素包含的文本或者None
元素沒有文本的Python 。請注意,文本不包含可能的子元素的文本,也不包含子項之間或之間的文本。另請注意,在XML空格中很重要,所以文本還包含縮進和換行符。要獲取可能子項的文本(可選擇空格標準化),請使用「 獲取元素文本」關鍵字。
$ {1st} = | 獲取元素 | $ {} XML | 第一 |
應該是平等的 | $ {} 1st.text | 文本 | |
$ {2nd} = | 獲取元素 | $ {} XML | 第二/兒童 |
應該是平等的 | $ {} 2nd.text | $ {無} | |
$ {p} = | 獲取元素 | $ {} XML | HTML / P |
應該是平等的 | $ {} p.text | \ n $ {SPACE * 6}帶$ {SPACE}的文字 |
在下一個打開或關閉標記以前的元素以後的文本。Python None
若是元素沒有尾部。與此相似text
,還tail
包含可能的縮進和換行符。
$ {b} = | 獲取元素 | $ {} XML | HTML / P / B |
應該是平等的 | $ {} b.tail | $ {SPACE}和$ {空白} |
包含元素屬性的Python字典。
$ {2nd} = | 獲取元素 | $ {} XML | 第二 |
應該是平等的 | $ {2nd.attrib [ '身份證']} | 2 | |
$ {3rd} = | 獲取元素 | $ {} XML | 第三 |
應該是空的 | $ {} 3rd.attrib |
ElementTree和lxml經過將名稱空間URI添加到所謂的Clark Notation中的標記名稱來處理XML文檔中可能的名稱空間。這對於xpaths來講是不方便的,默認狀況下,這個庫會剝離這些名稱空間並將它們移動到xmlns
屬性。經過將keep_clark_notation
參數傳遞給Parse XML關鍵字能夠避免這種狀況。或者,Parse XML支持使用strip_namespaces
參數徹底剝離命名空間信息。下面更詳細地討論不一樣方法的優缺點。
若是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 | {http://www.w3.org/1999/XSL/Transform}stylesheet | |
元素應該存在 | $ {}根 | {http://www.w3.org/1999/XSL/Transform}template/{http://www.w3.org/1999/xhtml}html | |
應該是空的 | $ {} root.attrib |
正如您所看到的,在標記名稱中包含名稱空間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 | http://www.w3.org/1999/XSL/Transform | |
元素屬性應該是 | $ {}根 | XMLNS | 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和不使用時都處理名稱空間。可是,與標準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 | 1 |
元素屬性應該是 | $ {}根 | {HTTP://my.ns} ID | 2 |
某些關鍵字接受以布爾值true或false處理的參數。若是這樣的參數以字符串形式給出,則若是它是空字符串或不區分大小寫,則被視爲false false
,none
或no
。不管其值如何,其餘字符串都被視爲true,其餘參數類型使用與Python相同的規則進行測試。
真實的例子:
解析XML | $ {} XML | keep_clark_notation =真 | #字符串一般是正確的。 |
解析XML | $ {} XML | keep_clark_notation = YES | #與上述相同。 |
解析XML | $ {} XML | keep_clark_notation = $ {TRUE} | #Pcthon True 是真的。 |
解析XML | $ {} XML | keep_clark_notation = $ {42} | #0之外的數字爲真。 |
錯誤的例子:
解析XML | $ {} XML | keep_clark_notation =假 | #String false 爲false。 |
解析XML | $ {} XML | keep_clark_notation =無 | #字符串no 也是false。 |
解析XML | $ {} XML | keep_clark_notation = $ {EMPTY} | #Empty字符串爲false。 |
解析XML | $ {} XML | keep_clark_notation = $ {FALSE} | #Python False 是假的。 |
在Robot Framework 2.9以前,全部非空字符串(包括false
和no
)都被認爲是真的。none
在Robot Framework 3.0.3中考慮false是新的。
參數 | 文檔 |
---|---|
use_lxml =假 | 導入庫,啓用了可選的lxml模式。 默認狀況下,此庫使用Python的標準ElementTree模塊來解析XML。若是 使用lxml須要在系統上安裝lxml模塊。若是啓用了lxml模式但未安裝模塊,則此庫將發出警告並恢復使用標準ElementTree。 對lxml的支持是Robot Framework 2.8.5中的新增功能。 |
添加元素 · 清除元素 · copy元素 · 元素屬性應該是 · 元素屬性應該匹配 · 元素應該存在 · 元素應該不存在 · 元素應該沒有屬性 · 元素文字應該 · 元素文本應該匹配 · 元至字符串 · 元素應該相等 · 元素應該匹配 · 評估Xpath · 獲取子元素 · 獲取元素 ·獲取元素屬性 · 獲取元素屬性 · 獲取元素計數 · 獲取元素文本 · 獲取元素 · 獲取元素文本 · 日誌元素 · 解析Xml · 刪除元素 · 刪除元素屬性 · 刪除元素屬性 · 刪除元素 · 刪除元素屬性 · 刪除元素屬性 · 保存Xml · 設置元素屬性 · 設置元素標記 · 設置元素文本 ·設置元素屬性 · 設置元素標籤 · 設置元素文本
關鍵詞 | 參數 | 文檔 | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
添加元素 | source, element, index = None, xpath =。 | 將子元素添加到指定的元素。 要使用 所述 使用示例
|
|||||||||||||||||||||||||||||||||||
清除元素 | source, xpath =。, clear_tail =假 | 清除指定元素的內容。 要清除的元素使用 清除元素意味着刪除其文本,屬性和子元素。默認狀況下不會刪除元素的尾部文本,但能夠經過給出 使用示例
使用「 刪除元素」刪除整個元素。 |
|||||||||||||||||||||||||||||||||||
複製元素 | source, xpath =。 | 返回指定元素的副本。 要複製的元素使用 若是以後修改了副本或原始元素,則更改對另外一個沒有影響。 使用示例
|
|||||||||||||||||||||||||||||||||||
元素屬性應該是 | source, name, expected,xpath =。, 消息=無 | 驗證指定的屬性是否爲 使用 若是 爲了測試元素沒有某個屬性,Python 使用示例
|
|||||||||||||||||||||||||||||||||||
元素屬性應該匹配 | 來源, 名稱, 模式, xpath =。,消息=無 | 驗證指定的屬性是否匹配 此關鍵字的做用與「 元素屬性應該是的」徹底相同,只是能夠將指望值做爲元素屬性必須匹配的模式給出。 模式匹配與shell中的匹配文件相似,而且始終區分大小寫。在模式中,'*'匹配任何內容和'?' 匹配任何單個字符。 使用示例
|
|||||||||||||||||||||||||||||||||||
元素應該存在 | source, xpath =。, 消息=無 | 驗證一個或多個元素是否與給定元素匹配 參數 |
|||||||||||||||||||||||||||||||||||
元素不該該存在 | source, xpath =。, 消息=無 | 驗證沒有元素與給定的匹配 參數 |
|||||||||||||||||||||||||||||||||||
元素不該該有屬性 | source, name, xpath =。, 消息=無 | 驗證指定的元素是否沒有屬性 使用 若是指定的元素具備屬性,則關鍵字將失敗 使用示例
|
|||||||||||||||||||||||||||||||||||
元素文本應該是 | source, expected, xpath =。,normalize_whitespace = False,message = None | 驗證指定元素的文本是否爲 驗證文本的元素使用 要使用與Get Element Text相同的邏輯從指定的元素獲取要驗證的文本。這包括使用該 若是元素的文本等於 使用示例
|
|||||||||||||||||||||||||||||||||||
元素文本應該匹配 | source, pattern, xpath =。,normalize_whitespace = False,message = None | 驗證指定元素的文本是否匹配 此關鍵字的工做方式與「 元素文本應該」徹底相同,只是能夠將指望值做爲元素文本必須匹配的模式給出。 模式匹配與shell中的匹配文件相似,而且始終區分大小寫。在模式中,'*'匹配任何內容和'?' 匹配任何單個字符。 使用示例
|
|||||||||||||||||||||||||||||||||||
元素到字符串 | source, xpath =。, encoding =無 | 返回指定元素的字符串表示形式。 要轉換爲字符串的元素是使用 默認狀況下,字符串以Unicode格式返回。若是 |
|||||||||||||||||||||||||||||||||||
要素應該是平等的 | source, expected,exclude_children = False,normalize_whitespace = False | 驗證給定 兩者 若是 驗證給定元素內的全部文本,但不包括它們以外的可能文本。默認狀況下,文本必須徹底匹配,但設置 使用示例
最後一個示例可能看起來有點奇怪,由於該 另請參見元素應匹配。 |
|||||||||||||||||||||||||||||||||||
元素應該匹配 | source, expected,exclude_children = False,normalize_whitespace = False | 驗證給定 此關鍵字的做用與Elements Should Be Equal徹底相同,只是指望值中的文本和屬性值能夠做爲模式給出。 模式匹配與shell中的匹配文件相似,而且始終區分大小寫。在模式中,'*'匹配任何內容和'?' 匹配任何單個字符。 使用示例
有關更多示例,請參閱元素應該相等。 |
|||||||||||||||||||||||||||||||||||
評估Xpath | 來源, 表達, 上下文=。 | 計算給定的xpath表達式並返回結果。 使用 要評估的xpath表達式做爲 使用示例
僅當在導入庫時使用lxml模式時,此關鍵字纔有效。機器人框架2.8.5中的新功能。 |
|||||||||||||||||||||||||||||||||||
獲取子元素 | source, xpath =。 | 以列表形式返回指定元素的子元素。 要返回的子元素使用 返回指定元素的全部直接子元素。若是元素沒有子元素,則返回空列表。 使用示例
|
|||||||||||||||||||||||||||||||||||
獲取元素 | source, xpath =。 | 返回 它 若是多於或少於一個元素匹配,則關鍵字失敗 使用示例
當須要整個結構時,建議使用Parse XML來解析XML。若是須要配置XML命名空間的處理方式,則必須使用它。 許多其餘關鍵字在內部使用此關鍵字,而且一般將修改XML的關鍵字記錄到二者以修改給定的源並返回它。若是源是以字符串形式給出,則修改源不適用。而後返回基於字符串解析而後修改的XML結構。 |
|||||||||||||||||||||||||||||||||||
獲取元素屬性 | source, name, xpath =。, 默認=無 | 返回指定元素的命名屬性。 要使用
使用示例
|
|||||||||||||||||||||||||||||||||||
獲取元素屬性 | source, xpath =。 | 返回指定元素的全部屬性。 要返回的元素使用 屬性做爲Python字典返回。它是原始屬性的副本,所以修改它對XML結構沒有影響。 使用示例
使用「 獲取元素屬性」獲取單個屬性的值。 |
|||||||||||||||||||||||||||||||||||
獲取元素計數 | source, xpath =。 | 返回並記錄給定 參數 |
|||||||||||||||||||||||||||||||||||
獲取元素文本 | source, xpath =。,normalize_whitespace = False | 返回元素的全部文本,多是空格規範化的。 要返回的文本的元素是使用 此關鍵字返回指定元素的全部文本,包括其子元素和孫子元素包含的全部文本。若是元素沒有文本,則返回空字符串。所以,返回的文本並不老是與元素的text屬性相同。 默認狀況下,元素內的全部空格(包括換行符和縮進)都按原樣返回。若是 使用示例
|
|||||||||||||||||||||||||||||||||||
獲取元素 | source, xpath | 返回 它 匹配的元素將 使用示例
|
|||||||||||||||||||||||||||||||||||
獲取元素文本 | source, xpath,normalize_whitespace = False | 返回 要返回的文本使用 使用與Get Element Text相同的邏輯返回匹配元素的文本。這包括使用該 使用示例
|
|||||||||||||||||||||||||||||||||||
日誌元素 | source, level = INFO, xpath =。 | 記錄指定元素的字符串表示形式。 使用 還會返回記錄的字符串。 |
|||||||||||||||||||||||||||||||||||
解析Xml | source, keep_clark_notation = False, strip_namespaces = False | 將給定的XML文件或字符串解析爲元素結構。 的 正如在處理XML名稱空間部分中所討論的那樣,默認狀況下,此關鍵字會刪除ElementTree添加到標記名稱並將其移動到 若是要徹底刪除命名空間信息,以便即便保存XML也不包含它,您能夠爲 例子:
|
|||||||||||||||||||||||||||||||||||
刪除元素 | source, xpath =, remove_tail = False |
使用與Get Element關鍵字相同的語義 若是關鍵字 默認狀況下不會刪除元素的尾部文本,但能夠經過給出 使用示例
|
|||||||||||||||||||||||||||||||||||
刪除元素屬性 | source, name, xpath =。 |
要使用 刪除不存在的屬性並不是失敗。使用「 刪除元素屬性」刪除全部屬性,使用「 設置元素屬性」設置它們。 使用示例
只能從單個元素中刪除屬性。使用「 刪除元素屬性」可在一次調用中刪除多個元素的屬性。 |
|||||||||||||||||||||||||||||||||||
刪除元素屬性 | source, xpath =。 | 從指定元素中刪除全部屬性。 要使用 使用「 刪除元素屬性」刪除單個屬性,使用「 設置元素屬性」設置它們。 使用示例
只能從單個元素中刪除屬性。使用「 刪除元素屬性」可在一次調用中刪除多個元素的全部屬性。 |
|||||||||||||||||||||||||||||||||||
刪除元素 | source, xpath =, remove_tail = False |
要使用與Get Elements關鍵字相同的語義 若是 默認狀況下不會刪除元素的尾部文本,但可使用 使用示例
|
|||||||||||||||||||||||||||||||||||
刪除元素屬性 | source, name, xpath =。 |
與刪除元素屬性相似,但刪除與給定匹配的全部元素的屬性 Robot Framework 2.8.6中的新功能。 |
|||||||||||||||||||||||||||||||||||
刪除元素屬性 | source, xpath =。 | 從指定元素中刪除全部屬性。 與刪除元素屬性相似,但刪除與給定匹配的全部元素的全部屬性 Robot Framework 2.8.6中的新功能。 |
|||||||||||||||||||||||||||||||||||
保存Xml | 源, 路徑, 編碼= UTF-8 | 將給定元素保存到指定文件。
保存元素的文件用 生成的XML文件可能與原始文件不徹底相同:
若是您只須要元素的字符串表示,請使用Element To String。 |
|||||||||||||||||||||||||||||||||||
設置元素屬性 | 來源, 名稱, 價值, xpath =。 |
要使用 既能夠設置新屬性又能夠覆蓋現有屬性。使用「 刪除元素屬性」或「 刪除元素屬性 」將其刪除。 使用示例
只能設置單個元素的屬性。使用「 設置元素屬性」在一次調用中設置多個元素的屬性。 |
|||||||||||||||||||||||||||||||||||
設置元素標記 | source, tag, xpath =。 | 設置指定元素的標記。 要使用 使用示例
只能設置單個元素的標記。使用「 設置元素標記」在一次調用中設置多個元素的標記。 |
|||||||||||||||||||||||||||||||||||
設置元素文本 | source, text = None, tail = None, xpath =。 | 設置指定元素的文本和/或尾部文本。 要使用 僅當給出新值 使用示例
只能設置單個元素的文本/尾部。使用「 設置元素文本」在一次調用中設置多個元素的文本/尾部。 |
|||||||||||||||||||||||||||||||||||
設置元素屬性 | 來源, 名稱, 價值, xpath =。 |
與Set Element Attribute相似,但設置與給定元素匹配的全部元素的屬性 Robot Framework 2.8.6中的新功能。 |
|||||||||||||||||||||||||||||||||||
設置元素標記 | source, tag, xpath =。 | 設置指定元素的標記。 與Set Element Tag相似,但設置與給定匹配的全部元素的標記 Robot Framework 2.8.6中的新功能。 |
|||||||||||||||||||||||||||||||||||
設置元素文本 | source, text = None, tail = None, xpath =。 | 設置指定元素的文本和/或尾部文本。 與Set Element Text相似,但設置與給定元素匹配的全部元素的文本或尾部 Robot Framework 2.8.6中的新功能。 |
共有37個關鍵字。
由Libdoc於2018-04-25 23:41:29 生成。