【问题标题】:Selecting a HTML node based on text containing space根据包含空格的文本选择 HTML 节点
【发布时间】:2014-07-30 07:23:50
【问题描述】:

我正在使用 Xpath 根据网页中的文本选择一个元素,只要文本选择器不包含空格,它就可以工作。

When the selector does contain spaces, however, it fails.
我看过:Locating the node by value containing whitespaces using XPath
和xpath expression to remove whitespace
和how to get the normalize-space() xpath function to work? 但这没有用。

失败的 html 示例:

<tr style="vertical-align:top;">
        <td style="height:15px;"></td>
        <td class="cs9D60B82">Yellow&nbsp;Fees</td>
        <td class="cs9D60B82">Group1</td>
        <td class="cs9D60B82">Yes</td>
        <td class="cs9DB0B82"></td>
        <td class="cs9D60B82">QD&nbsp;1,234.56</td>
</tr>

这些工作,但也可以返回以相同文本开头的其他节点:

"//td[starts-with( normalize-space(), 'Yellow')]"  
"//td[starts-with(text(), 'Yellow')]"

这些不起作用:

 "//td[normalize-space(.) = 'Yellow Fees']"  
 "//td[translate(text(), '&#9;&#xA;&#xD;','') = 'YellowFees']"
 "//td[translate(text(), ' &#9;&#xA;&#xD;','') = 'Yellow&#xA0;Fees']"  
 "//td[translate(text(), '&nbsp;','') = 'YellowFees']"  
 "//td[translate(text(), '&#9;&#xA;&#xD;','') = 'Yellow&nbsp;Fees']"
 "//td[matches( text(), 'Yellow&nbsp;Fees')]"

【问题讨论】:

    标签: c# html xpath


    【解决方案1】:

    我在https://groups.google.com/forum/#!topic/selenium-users/IJP6VKnnEeQ找到了答案

    "//td[starts-with( normalize-space(), 'Yellow\u00A0Fees')]"  
    

    有效!

    【讨论】:

    • 我无法使用xpathtester.com/xpath 获得类似的结果。我什至注册了实体&lt;!DOCTYPE html [``]&gt;
    • @JoelM.Lamsen 你尝试过类似//td[contains(text(),'Yellow Fees')]的方法
    • @callisto:啊,问题是 &nbsp 不会转换为空白,它会转换为不可破坏的空白 - 当然,愚蠢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多