You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Robot Framework提取XML中report节点title及Condition属性失败咨询

Robot Framework XML数据提取正确实现方案

前置配置

  • 测试套件开头导入XML库:
*** Settings ***
Library    XML

核心实现逻辑

假设你的XML结构示例如下:

<root>
    <report title="设备巡检报告202405">
        <Condition type="异常">CPU使用率连续3天超过90%</Condition>
        <Condition type="提醒">磁盘剩余空间不足20%</Condition>
    </report>
    <report title="设备巡检报告202406">
    </report>
</root>

完整提取逻辑如下:

*** Test Cases ***
提取XML指定字段并比对
    # 1. 解析XML文件,返回根节点对象
    ${xml_root}    Parse XML    你的目标文件路径.xml

    # 2. 提取所有report节点
    @{all_report_nodes}    Get Elements    ${xml_root}    xpath=.//report

    # 3. 遍历每个report节点提取属性和子节点数据
    FOR    ${report_node}    IN    @{all_report_nodes}
        # 提取report的title属性:用Get Element Attribute,不需要加@前缀
        ${report_title}    Get Element Attribute    ${report_node}    title
        Log    提取到report标题:${report_title}

        # 4. 查找当前report下的Condition子节点,关闭空结果报错
        @{condition_nodes}    Get Elements    ${report_node}    xpath=./Condition    fail_on_empty=${False}

        # 存在Condition节点时提取对应数据
        IF    ${len(condition_nodes)} > 0
            FOR    ${cond_node}    IN    @{condition_nodes}
                ${cond_type}    Get Element Attribute    ${cond_node}    type
                ${cond_content}    Get Element Text    ${cond_node}
                Log    Condition类型:${cond_type},内容:${cond_content}

                # 此处可添加和网页端抓取数据的断言逻辑,例如:
                # ${page_cond_content}    Get Text    网页元素定位
                # Should Be Equal    ${cond_content}    ${page_cond_content}
            END
        END
    END

之前操作的问题说明

  • 取节点属性不能用Get Element Text,该关键字仅用于获取节点包裹的文本内容,属性提取需使用Get Element Attribute,直接传入属性名即可,无需加@前缀
  • 查找Condition节点时要限定在当前遍历的report节点下,不要全局查找,添加fail_on_empty=${False}参数避免无对应子节点时直接抛出异常中断执行

内容的提问来源于stack exchange,提问作者Saruto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 01:06:04