【问题标题】:How to write a XPath that selects a attribute value in two different levels如何编写在两个不同级别中选择属性值的 XPath
【发布时间】:2014-10-01 07:28:58
【问题描述】:

有 2 个表,我需要选择“date1”文本,该文本可能位于第一个表的 tr/td/a 或第二个表的 tr/td 中。这种场景怎么写xpath?

<div>
    <table>
       <tbody>
           <tr>
             <td id="1">
                <a style="font-style:normal;font-weight:bold;" onclick="return" class="PTL"> </a>
             </td>  
             <td id="1">
                <a style="font-style:normal;font-weight:bold;" onclick="return" class="PTL">date1</a>
             </td>  
             <td id="1">
                <a style="font-style:normal;font-weight:bold;" onclick="return" class="PTL">date1</a>
             </td>
             <td id="1">
                <a style="font-style:normal;font-weight:bold;" onclick="return" class="PTL">date2</a>
             </td>
             <td id="1">
                <a style="font-style:normal;font-weight:bold;" onclick="return" class="PTL">date3</a>
             </td>
           </tr>
       </tbody>
    </table>
    <table>
       <tbody>
           <tr>
              <td id="1" style="font-style:normal;font-weight:bold;" class="PTCH"> </td>
              <td id="1" style="font-style:normal;font-weight:bold;" class="PTCH">date1</td>
              <td id="1" style="font-style:normal;font-weight:bold;" class="PTCH">date2</td>
              <td id="1" style="font-style:normal;font-weight:bold;" class="PTCH">date3</td>
           </tr>
       </tbody>
    </table>
</div>

我试过了,但这会选择整行。我想选择一个特定的列

xpath=(//table)[2]//tr[td[contains(text(),'date1') and not(text()=' ')] or td/a[contains(text(),'date1') and not(text()=' ')]]

【问题讨论】:

  • 对我投反对票。没有证据表明您自己尝试过任何事情或研究过这个主题。这个问题只是一个需求转储。
  • 我对此进行了大量研究,但无法找到解决方案。你能看看这个
  • @udeshplus 您说您想选择date1 文本,但在您的尝试中,您选择了tr 元素。你真正想做的是什么?请清楚地描述您的要求。
  • @JLRishe 我想选择“date1”文本。就我而言,有两张桌子。对象路径“tbody/tr/td/a”中的第一个表“date1”文本和对象路径“tbody/tr/td”中的第二个表。所以我需要编写一个 xpath 来使用“或”条件为两种场景选择“date1”文本。对不起我的英语不好

标签: java html xml xpath selenium-webdriver


【解决方案1】:

在您发布的答案中,您实际上是在选择 td 元素。您说您要选择“date1”文本,但如果您已经知道它是什么,那么选择该文本是没有意义的。

以下是您发布的内容的更简单方法:

//table//tr/td[contains(., 'date1')]

或者,如果您只想包含第一个表格(就像您在帖子中所做的那样):

(//table)[1]//tr/td[contains(., 'date1')]

关于您在下面的评论,如果您想查找属性中的值,请执行以下操作:

(//table)[1]//tr/td[descendant-or-self::*[contains(@style, 'bold')]]

或者,如果您想选择具有style 属性和bold 的实际元素:

(//table)[1]//tr/td/descendant-or-self::*[contains(@style, 'bold')]

【讨论】:

  • 您的答案适用于文本内容,但如果我尝试使用样式属性,这样它就行不通了。 xpath=(//table)[1]//tr/td[contains(., 'bold')] 但是使用我的解决方案可以。请说清楚。 xpath=(//table)[1]//tr/td[contains(@style,'bold') and not(text()=' ') or a[contains(@style,'bold') and not(text()=' ')]]
【解决方案2】:

//a 您可以将其复制到文本文件中并将其更改为 html 使用 firebug 查找路径和 xpath 检查器(Fire Fox 插件)对于初学者来说非常容易

【讨论】:

    【解决方案3】:

    经过多次尝试,我想出了这个方法,并且可以根据需要进行工作。谢谢大家。

    xpath=(//table)[1]//tr/td[contains(text(),'date1') and not(text()=' ') or a[contains(text(),'date1') and not(text()=' ')]]
    

    【讨论】:

      猜你喜欢
      • 2011-07-27
      • 1970-01-01
      • 2011-01-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多