属性的 XPath 部分已知

发布于 2024-07-20 06:39:52 字数 968 浏览 5 评论 0 原文

我知道文档中属性的部分值,但不知道全部。 我可以用一个字符来表示任何值吗? 例如,输入的标签值为“A.选择1”。 我知道它说“选择 1”,但不知道“选择 1”之前是否会说“A.”或“B.”。 下面是相关的 HTML。 输入和标签还有其他属性,但每次渲染页面时它们都不相同,因此我不能将它们用作参考:

<tr>
<td><input type="checkbox" /><label>A. Choice 1</label></td>
</tr><tr>
    <td><input type="checkbox" /><label>B. Choice 2</label></td>
</tr><tr>
<td><input type="checkbox" /><label>C. Choice 3</label></td>
</tr><tr>
     <td><input type="checkbox" /><label>D. Choice 4</label></td>
</tr>

这是 XPath 表达式我用来选择值为“Choice 1”的标签旁边的输入,除了 A 位于它的前面HTML:

//td[label="Choice 1"]/input

我不知道 HTML 中的 A 是 A、B 还是 C 等。但我确实知道正确的输入旁边总是有“选项 1”文本。 如果标签包含选择1,而不是等于选择1,我该如何说选择它?

I know the partial value of an attribute in a document, but not the whole thing. Is there a character I can use to represent any value? For example, a value of a label for an input is "A. Choice 1". I know it says "Choice 1", but not whether it will say "A. " or "B. " before the "Choice 1". Below is the relevant HTML. There are other attributes for the input and the label, but they are not the same every time the page is rendered, so I can't use them as references:

<tr>
<td><input type="checkbox" /><label>A. Choice 1</label></td>
</tr><tr>
    <td><input type="checkbox" /><label>B. Choice 2</label></td>
</tr><tr>
<td><input type="checkbox" /><label>C. Choice 3</label></td>
</tr><tr>
     <td><input type="checkbox" /><label>D. Choice 4</label></td>
</tr>

This is the XPath expression I'm using to select the input next to the label with the value of "Choice 1", except that the A is in front of it in the HTML:

//td[label="Choice 1"]/input

I don't know if the A in the HTML will be an A, B, or C, etc. But I do know that the correct input will always have the Choice 1 text next to it. How do I say to select it if the label contains Choice 1, as opposed to being equal to choice 1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不…忘初心 2024-07-27 06:39:52

您的 XPath 表达式应如下所示:

//td[contains(@label, 'Choice 1')]/input

您选择标签包含 Choice 1 的所有 td 元素,然后选择其中的 input 元素这些 td 元素。

编辑:托马拉克的评论正确地建议了一项改进,以防止与“Choice 11”(或“Choice 12345”,...)的比赛。

Your XPath expression should look like this:

//td[contains(@label, 'Choice 1')]/input

You select all td elements that have a label that contains Choice 1 and then you select the input elements inside these td elements.

EDIT: Tomalak's comment correctly suggests an improvement to prevent a match against 'Choice 11' (or 'Choice 12345', ...).

在你怀里撒娇 2024-07-27 06:39:52

在尝试找到一种方法来测试节点是否具有不为空的属性“align”或包含文本值“text-align”的属性“style”时,找到了罗纳德的解决方案。 这些是有问题的“节点”:

<node> 
  <p>This is left-aligned.</p> 
  <div align="left" >This is aligned LEFT using HTML attribute.</div> 
  <p style="text-align: center;" >This is centered using CSS style attribute.</p> 
  <div align="center" >This is CENTERED.</div> 
  <p style="text-align: right;" >This is right-aligned.</p> 
</this>

这个 xpath 表达式起作用了——感谢罗纳德的回答,它为我指明了正确的方向:

//*[contains(@style, 'align') or @align!='']

Found Ronald's solution while trying to find a way to test whether a node has either an attribute 'align' that is not empty OR an attribute 'style' that contains text value 'text-align'. These are the 'nodes' in question:

<node> 
  <p>This is left-aligned.</p> 
  <div align="left" >This is aligned LEFT using HTML attribute.</div> 
  <p style="text-align: center;" >This is centered using CSS style attribute.</p> 
  <div align="center" >This is CENTERED.</div> 
  <p style="text-align: right;" >This is right-aligned.</p> 
</this>

This xpath expression worked -- thanks to Ronald's answer that pointed me in the right direction:

//*[contains(@style, 'align') or @align!='']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文