简体   繁体   English

如何编写在两个不同级别选择属性值的 XPath

[英]How to write a XPath that selects a attribute value in two different levels

There are 2 tables and I need to select the "date1" text which might be in the tr/td/a in the 1st table or tr/td in the 2nd table.有 2 个表,我需要选择可能位于第一个表中的 tr/td/a 或第二个表中的 tr/td 中的“date1”文本。 How to write a xpath for this kind of scenario?如何为这种场景编写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>

I tried this but this select the entire row.我试过这个,但这选择了整行。 I want to select a specific column我想选择一个特定的列

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

In the answer you posted, you are actually selecting a td element.在您发布的答案中,您实际上是在选择td元素。 You say you want to select the "date1" text, but there is no point in selecting the text if you already know what it is.您说要选择“date1”文本,但如果您已经知道它是什么,则选择该文本没有意义。

The following is a simpler approach to what you've posted:以下是您发布的内容的更简单方法:

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

Or if you only want to include the first table (as you're doing in your post):或者,如果您只想包含第一个表格(就像您在帖子中所做的那样):

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

Regarding your comment below, if you want to find a value that is in an attribute, do this:关于您在下面的评论,如果您想查找属性中的值,请执行以下操作:

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

Or if you want to select the actual element that has the style attribute with bold in it:或者,如果您想选择具有bold style属性的实际元素:

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

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

After number of attempts I came up with this and its working as I needed.经过多次尝试,我想出了这个并按我的需要工作。 Thanks all.谢谢大家。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM