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

如何获取Robot Framework指定库(如BuiltIn)的所有可用关键字列表

获取Robot Framework指定库的所有可用关键字列表

要提取Robot Framework指定库(比如BuiltIn)的所有可用关键字,可以通过调用robot.libdoc生成库的XML文档,再解析XML提取关键字。以下是实现代码:

from robot.libdoc import libdoc
import xml.etree.ElementTree as ET

def get_defined_keywords(library_name):
    try:
        # 生成库文档并保存为XML文件
        output_path = 'output.xml'  # 输出XML文件路径
        libdoc(library_name, output_path)
        
        # 解析XML文件提取关键字
        with open(output_path, 'r') as xml_file:
            xml_content = xml_file.read()

            # 解析XML内容,提取所有已定义的关键字
            root = ET.fromstring(xml_content)
            keywords = [kw.text for kw in root.findall('.//kw/name')]
            return keywords

    except Exception as e:
        print(f"错误: {e}")
        return None

print(get_defined_keywords("BuiltIn"))

代码说明:

  • 借助libdoc工具生成目标库的XML格式文档,该工具是Robot Framework官方提供的库文档生成工具
  • 使用xml.etree.ElementTree解析XML文件,通过XPath表达式.//kw/name定位所有关键字的名称节点
  • 加入异常捕获逻辑,输出错误信息的同时避免程序直接崩溃

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 19:28:09