0

我是机器人框架的 XML 库的新手。我尝试解析 xml 以获取其中的值,但它只会获取第一个元素。所以我的 XML 是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Header>
    <firstElement>
        <secondElement>
            <myValue>...</myValue>
            <mySecondValue>...</mySecondValue>
            ...
        </secondElement
    </firstElement>
  </S:Header>
  <S:Body>
  ...
  </S:Body>
</S:Envelope>

我非常简短的机器人框架测试如下所示:

 ${xml}=    Parse Xml    path/to/xml
 ${first}=    get element    ${xml}    myValue
 Log  ${first}

但是在解析 XML 时,它会像这样记录它:

INFO : ${xml} = <Element 'Envelope' at 0x00000000042B67C8>

当然,我在解析的 xml 中获取值的所有尝试都失败了,我得到:

FAIL : No element matching 'myValue' found.

我做错了什么?

4

1 回答 1

1

问题在于您用于查找元素的 xpath,请看这里:https ://robotframework.org/robotframework/latest/libraries/XML.html#Finding%20elements%20with%20xpath

它应该如下所示:

    ${x}=    Parse Xml    ${xml}    
    ${el_my_value}=    Get Element    ${x}    .//myValue
    Log  ${el_my_value}
    ${first_text}=    Get Element Text    ${el_my_value} 

请注意.//myValue.

另外,如果要获取元素text,则需要使用 keyword Get Element Text

所以整个工作示例和结果:

*** Settings ***
Library    XML
Variables    ../../Resources/xml_test.py

*** Test Cases ***
Test XML Parsing
    ${x}=    Parse Xml    ${xml}    
    ${el_my_value}=    Get Element    ${x}    .//myValue
    Log  ${el_my_value}
    ${first_text}=    Get Element Text    ${el_my_value}    

在此处输入图像描述

于 2020-06-02T08:26:36.490 回答