在Robot Framework中,我正在尝试检查源代码中是否存在“ XXX”,如下所示:
<button class='btn-aaa'>
<span class='bbb'>
XXX
</button>
我尝试使用以下代码,但失败了:
Page Should Contain Element | //button/span/following-sibling::text()="XXX"
---> Valid xpath but encountering error in Robot Framework with error message "InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //button/span/following-sibling::text()="XXX" because of the following error: TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type."
Page Should Contain Element | //button/span[@class='bbb']/following-sibling::contains(text(),"XXX")
---> Invalid xpath
Page Should Contain Element | //button/span[@class='bbb']/following-sibling::[contains(text(),"XXX")]
---> Invalid xpath
有人可以建议在Robot Framework中使用正确的xpath检查“ XXX”的正确方法吗?
以下是所使用工具的信息:
编辑:
我真的很感谢你们所有人都在努力提供帮助。非常感谢你。 我会尽力提供尽可能多的信息,以便您也能帮助我。
我尝试过:
//button[normalize-space()='View Data']
不幸的是,它什么也没发现。
还有其他按钮,但只有一个带有文本“查看数据”的按钮。
这里是更详细的代码。我不知道与之前提到的简化版本相比是否有帮助:
<button class="btn btn-submit float-left" type="button" onclick="return doActionParam('https://www.blabla.com/blabla',{op:'viewBla',blaId:'wxyz', activeTab:getActiveTabName()})">
<!--groupParent.-->
<span class="bongobongo"></span>
View Data
<button>
希望这次信息会更有用。再次感谢。
其他信息:
浏览器:Mozilla Firefox 62.0.3(64位)
答案 0 :(得分:0)
您可以尝试以下xpath://button[normalize-space()='XXX']
您的测试应该是:Page Should Contain Element //button[normalize-space()='XXX']
答案 1 :(得分:0)
问题是您的页面不包含带有文本“ XXX”的范围。它实际具有的是“换行符,XXX,换行符”。而且,span和按钮都没有兄弟姐妹(因此也没有跟随兄弟姐妹)。
您可以忽略normalize-space
周围的空格。例如,当我创建与您的问题相同的HTML文档时,以下内容对我有用:
Page should contain element //button[normalize-space()='XXX']
只需将XXX
替换为View Data
,就可以在代码中的更新示例上起作用:
Page should contain element //button[normalize-space()='View Data']