已知属性的 XPath 部分 [英] XPath partial of attribute known

查看:29
本文介绍了已知属性的 XPath 部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道文档中某个属性的部分值,但不知道全部.有没有我可以用来表示任何值的字符?例如,输入的标签值为A. Choice 1".我知道它说选择 1",但不知道它是否会在选择 1"之前说A."或B.".下面是相关的 HTML.input和label还有其他的属性,但是每次页面渲染的时候都不一样,所以不能作为参考:

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>

这是我用来选择标签旁边的输入的 XPath 表达式"Choice 1" 的值,除了 A 在 HTML 中它的前面:

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

我不知道 HTML 中的 A 是 A、B 还是 C 等.但我知道正确的输入旁边总会有选择 1 文本.如果标签包含选择1,而不是等于选择1,我怎么说选择它?

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?

推荐答案

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

Your XPath expression should look like this:

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

您选择所有标签包含 Choice 1td 元素,然后选择这些 td 中的 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.

编辑:Tomalak 的评论正确地提出了一项改进,以防止与Choice 11"(或Choice 12345",...)匹配.

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

这篇关于已知属性的 XPath 部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆