我知道文档中属性的部分值,但不知道全部。 我可以用一个字符来表示任何值吗? 例如,输入的标签值为“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?
发布评论
评论(2)
您的 XPath 表达式应如下所示:
您选择标签包含
Choice 1
的所有td
元素,然后选择其中的input
元素这些 td 元素。编辑:托马拉克的评论正确地建议了一项改进,以防止与“Choice 11”(或“Choice 12345”,...)的比赛。
Your XPath expression should look like this:
You select all
td
elements that have a label that containsChoice 1
and then you select theinput
elements inside thesetd
elements.EDIT: Tomalak's comment correctly suggests an improvement to prevent a match against 'Choice 11' (or 'Choice 12345', ...).
在尝试找到一种方法来测试节点是否具有不为空的属性“align”或包含文本值“text-align”的属性“style”时,找到了罗纳德的解决方案。 这些是有问题的“节点”:
这个 xpath 表达式起作用了——感谢罗纳德的回答,它为我指明了正确的方向:
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:
This xpath expression worked -- thanks to Ronald's answer that pointed me in the right direction: