我想获取给定库下所有可用关键字的列表,例如BuildIn
from robot.libdoc import libdoc
import xml.etree.ElementTree as ET
def get_defined_keywords(library_name):
try:
# Generate library documentation and save it to an XML file
output_path = 'output.xml' # Path to the output XML file
libdoc(library_name, output_path)
# Process the generated XML file to extract keywords
with open(output_path, 'r') as xml_file:
xml_content = xml_file.read()
# Parse the XML content to extract the defined keywords
root = ET.fromstring(xml_content)
keywords = [kw.text for kw in root.findall('.//kw/name')]
return keywords
except Exception as e:
print(f"Error: {e}")
return None
print(get_defined_keywords("BuiltIn"))
.
列表为空,因为任何标签
kw
在 XML 中都没有直接子标签 name
。
但是,查看生成的 XML 中的随机
kw
标记:
<kw name="Convert To Bytes"
...`
必要的信息,即关键字的名称,是标签的属性,而不是子标签。
例如,您可以通过以下方式获取姓名列表 更换
[kw.text for kw in root.findall('.//kw/name')]
到-->
[tag.attrib['name'] for tag in root.findall('.//kw')]
例如,
产生:
['Call Method', 'Catenate', 'Comment', 'Continue For Loop', 'Continue For Loop If', ...