首页 文章

创建一个检查两个属性的有效XPath

提问于
浏览
0

我正在使用FirePath为Behat自动化测试生成有效的XPath,并经常发现自己遇到此问题:

我需要为自动化测试生成一个XPath,即单击一个元素,但该路径包含两个部分,我需要检查以确认它是正确的

<div id="flash-success" class="alert-box with-icon info"> Operation note created. </div>

所以在上面的代码中,我可以使用任何这些有效的XPath,它们将导致 one 匹配节点:

//*[@id='flash-success']
//*[@class='alert-box with-icon info']
//*[contains(text(), 'Operation note created.')]

理想情况下,我想确认XPath检查两个部分,id / class和text,如下所示:

//*[@class='alert-box with-icon info']//*[contains(text(), 'Operation note created.')]

但这不是一个有效的XPath . 任何人都可以在这里说清楚,我已经尝试过阅读W3和这里的问题,但还没有找到解决方案

1 回答

  • 2

    如果你想找到一个元素,你的所有3个条件都必须是真的你可以写:

    //*[@id='flash-success'][@class='alert-box with-icon info'][contains(text(), 'Operation note created.')]
    

    要么

    //*[@id='flash-success' and @class='alert-box with-icon info' and contains(text(), 'Operation note created.')]
    

    如果你想找到一个元素你的任何条件都是真的你会写:

    //*[@id='flash-success' or @class='alert-box with-icon info' or contains(text(), 'Operation note created.')]
    

    作为旁注,通常在检查html中的类属性时,您会执行 contains(@class,'alert-box') ,因为通常有多个空格分隔的类,这些类通常是生成的,不必按任何顺序排列 .

相关问题