pythonxml:childer具有特定属性值的元素的xpath
我需要用于以下内容的xpath表达式: 标记为“test”的所有节点,其子节点标记为“status”,其属性名为“status”,值为“PASS” 因此,对于下面的xml,我需要获取id为“s1-t1”的节点。我应该能够修改它,这样就可以得到属性为pythonxml:childer具有特定属性值的元素的xpath,python,xml,xpath,robotframework,Python,Xml,Xpath,Robotframework,我需要用于以下内容的xpath表达式: 标记为“test”的所有节点,其子节点标记为“status”,其属性名为“status”,值为“PASS” 因此,对于下面的xml,我需要获取id为“s1-t1”的节点。我应该能够修改它,这样就可以得到属性为[@status='FAIL']的测试,而不是通过测试。我试过几件事都没用: “//test/[@status='PASS']”不返回任何内容 “//test/[status][@status='PASS']”不返回任何内容 “//test//[st
[@status='FAIL']
的测试,而不是通过测试。我试过几件事都没用:
不返回任何内容“//test/[@status='PASS']”
不返回任何内容“//test/[status][@status='PASS']”
抛出一个语法错误(“无效后代”)“//test//[status][@status='PASS']”
返回两个状态节点(一个用于测试状态,一个用于关键字状态)“//test//status[@status='PASS']”
返回正确的测试节点,但也返回套件节点“//test//status[@status='PASS']/../”
<?xml version="1.0" encoding="UTF-8"?>
<robot generated="20151111 16:29:28.630" generator="Robot 2.9.1 (Python 2.7.10 on cygwin)">
<suite source="/cygdrive/c/test/robot/thing.robot" id="s1" name="Thing">
<test id="s1-t1" name="Case1">
<kw name="Should Contain" library="BuiltIn">
<doc>Fails if ``item1`` does not contain ``item2`` one or more times.</doc>
<arguments>
<arg>hello</arg>
<arg>hell</arg>
</arguments>
<status status="PASS" endtime="20151111 16:29:28.689" starttime="20151111 16:29:28.689"></status>
</kw>
<status status="PASS" endtime="20151111 16:29:28.689" critical="yes" starttime="20151111 16:29:28.688"></status>
</test>
<test id="s1-t2" name="Case2">
<kw name="Should Contain" library="BuiltIn">
<doc>Fails if ``item1`` does not contain ``item2`` one or more times.</doc>
<arguments>
<arg>hello</arg>
<arg>elo</arg>
</arguments>
<msg timestamp="20151111 16:29:28.690" level="FAIL">'hello' does not contain 'elo'</msg>
<status status="FAIL" endtime="20151111 16:29:28.690" starttime="20151111 16:29:28.690"></status>
</kw>
<status status="FAIL" endtime="20151111 16:29:28.690" critical="yes" starttime="20151111 16:29:28.690">'hello' does not contain 'elo'</status>
</test>
<test id="s1-t3" name="Case3">
<kw name="Should Contain" library="BuiltIn">
<doc>Fails if ``item1`` does not contain ``item2`` one or more times.</doc>
<arguments>
<arg>hello</arg>
</arguments>
<msg timestamp="20151111 16:29:28.691" level="FAIL">Keyword 'BuiltIn.Should Contain' expected 2 to 4 arguments, got 1.</msg>
<status status="FAIL" endtime="20151111 16:29:28.691" starttime="20151111 16:29:28.691"></status>
</kw>
<status status="FAIL" endtime="20151111 16:29:28.691" critical="yes" starttime="20151111 16:29:28.691">Keyword 'BuiltIn.Should Contain' expected 2 to 4 arguments, got 1.</status>
</test>
<status status="FAIL" endtime="20151111 16:29:28.692" starttime="20151111 16:29:28.633"></status>
</suite>
<statistics>
<total>
<stat fail="2" pass="1">Critical Tests</stat>
<stat fail="2" pass="1">All Tests</stat>
</total>
<tag>
</tag>
<suite>
<stat fail="2" id="s1" name="Thing" pass="1">Thing</stat>
</suite>
</statistics>
<errors>
</errors>
</robot>
如果``item1``不包含``item2``一次或多次,则失败。
你好
地狱
如果``item1``不包含``item2``一次或多次,则失败。
你好
埃洛
“hello”不包含“elo”
“hello”不包含“elo”
如果``item1``不包含``item2``一次或多次,则失败。
你好
关键字'BuiltIn.Should Contain'应包含2到4个参数,得到1个。
关键字'BuiltIn.Should Contain'应包含2到4个参数,得到1个。
关键测试
所有测试
事情
您就快到了:
.//test[status/@status = "PASS"]
这正是所有带有标记“test”的节点,这些节点的子节点带有标记“status”,其属性名为“status”,值为“PASS”。
SyntaxError(“不能在元素上使用绝对路径”)
@ewok当然,添加了点。@ewok这是您正在使用的xml.etree.ElementTree
吗?如果可能的话,它有一个,切换到lxml.etree
。@ewok我担心您将不得不在test
元素上循环,并检查是否有一个status
元素包含所需的status
属性。Lmk,如果你需要一个例子。谢谢,不用客气。我仍然在使用root.findall()
,我需要使用root.xpath()
。谢谢