List of BuildIn robot keywords

49 views Asked by At

I want to get the list of all available keywords under a given library ,for exemple 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"))

.

1

There are 1 answers

0
Sam Maksymyshyn On

The list is empty because any tag kw doesn't have a direct child tag name in the XML.

But, look at the random kw tag from the generated XML:

<kw name="Convert To Bytes" ...`

The necessary info, the name of the keyword, is an attribute of the tag, not the child tag.

So, for example, you could take a list of names by replacing

[kw.text for kw in root.findall('.//kw/name')]

to -->

[tag.attrib['name'] for tag in root.findall('.//kw')]

e.g.,

that produces:

['Call Method', 'Catenate', 'Comment', 'Continue For Loop', 'Continue For Loop If', ...