Python - ElementTree- cannot use absolute path on element

前端 未结 1 1365
猫巷女王i
猫巷女王i 2021-02-03 21:42

I\'m getting this error in ElementTree when I try to run the code below:

SyntaxError: cannot use absolute path on element

My XML document looks

1条回答
  •  闹比i
    闹比i (楼主)
    2021-02-03 22:08

    Turns out I needed to say target.findall(".//StepText"). I guess anything without the '.' is considered an absolute path?

    Updated working code:

    def search():
        root = ET.parse(INPUT_FILE_PATH)
        for target in root.findall("//Script"):
            stepTexts = target.findall(".//StepText")
            for stepText in stepTexts:
                if FIND.lower() in stepText.text.lower():
                    print target.attrib['name'],' -- ',stepText.text
    

    0 讨论(0)
提交回复
热议问题