XPATH 使用多个 AND 语句与 CONTAINS 或 STARTS WITH

标签 xpath

<tr>
                <td>Principal</td>
                <td class="xh-highlight">Brian Jones</td>
            </tr>

我使用以下命令返回包含单词“princpal”的 td:

//td[starts-with(., "Princ")]/following-sibling::td
//td[contains(., 'Princ')]/following-sibling::td

两者都工作正常,但我循环访问的一些 html 页面使用不同的术语,因此我需要查找不同的术语。例如,有些人使用“headmaster”,甚至只使用“head”:

<tr>
                    <td>Headmaster</td>
                    <td class="xh-highlight">Mrs Baines</td>
                </tr>

我需要做的是使用某种 OR 语句,我一直在尝试这种变体:

//td[contains(., 'Princ' and contains ., 'head' and contains 'chief')]/following-sibling::td

因为我需要进行一些测试才能找到合适的。我无法让它返回任何结果。

最佳答案

尝试下面的表达式:

//td[contains(., 'Princ') or contains(., 'Head') or contains(., 'Chief')]/following-sibling::td

//td[matches(., ("Princ|Head|Chief"), "i")]/following-sibling::td

如果您的工具支持matches()

关于XPATH 使用多个 AND 语句与 CONTAINS 或 STARTS WITH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45817051/

相关文章:

java - 获取 XML 节点位置 - XPath?

xml - 在Elixir中获取XML中没有文本值的元素

xml - XQuery:同一模块中有两个 namespace ?

c# - 无法使用C#通过XPath查找元素

powershell - XPath表达式将不接受变量

python - 在 Python 中使用 Element Tree 合并 xml 文件

java - 基于高效 Camel 内容的路由器 : Route XML messages to the correct recipient based on contained tag with Java DSL

python - selenium.common.exceptions.NoSuchElementException : Message: no such element: Unable to locate element: {"method" :"css selector" ,"selector" :".ui flu~"}

java - 如何使用 Selenium Webdriver 单击元素

xml - 使用 Xpath 将 XML 节点提取到 Hive 表中