首页 > 解决方案 > 如何根据跨度内的文本获取跨度的以下元素

问题描述

如何使用跨度内的文本识别跨度的以下元素。在下面的代码中,我需要使用其文本识别第一个跨度以在<li>. 我可以使用它的类属性来识别跨度,但我不能用它contains(text(), 'System stopped operation')来识别跨度标签。我的想法是使用其类识别第一个跨度,然后找到以下内容<li>

<span class="text-danger">
      &nbsp;
      <img src="images/crit.gif" width="15" height="15" border="0" align="absbottom" alt="Critical Alarm" title="Critical Alarm">
      &nbsp;System stopped operation
    </span>
    <ul>
      <li>
        The system is not in operation.</li>
    </ul>
    <span class="text-warning">
      &nbsp;
      <img src="images/warn.gif" width="15" height="15" border="0" 
    align="absbottom" alt="Warning Alarm" title="Warning Alarm">
    &nbsp;System restarted
    </span>
    <ul>
      <li>
        System has been restarted.</li>
    </ul>

标签: htmlseleniumhtml-lists

解决方案


<li>要根据相应标签中的文本识别元素<span>,可以使用以下基于xpath的解决方案:

  • <li>带有文本的标识为系统未运行

    //span[contains(., 'System stopped operation')]//following::ul[1]/li
    
  • 要将<li>with 文本标识为System has been restarted

    //span[contains(., 'System restarted')]//following::ul[1]/li
    

推荐阅读