如何为特定节点编写条件 xpath select?
How do I write a conditional xpath select for specific nodes?
所以我在 chrome、autopagerizer 上添加了一个用户脚本。
目前我正在使用
//div[@class=\'entry-content\']/p[contains(.,\'chapter\') or contains(.,\'Chapter\') or preceding-sibling::p[contains(.,\'Chapter\')] and (following-sibling::p[contains(.,\'Translator Ramblings\')] or following-sibling::p[contains(.,\'Translating Ramblings\')] or following-sibling::p[contains(.,\'Ramblings\')] or following-sibling::p[starts-with(.,\'Translator\')] or following-sibling::p[starts-with(.,\'Translating\')])]
我想要一个条件参数,当上面的代码没有解决任何问题时,它会 select
//h3[contains(.,\'Share this\')]
相反。在这种情况下,我只希望 h3 被 selected。我不知道如何在 xpath 中写这个,我已经查找了其他相关问题,但他们没有解决这个问题。如果我不能用 xpath 解决这个问题,我将如何用 javascript 解决这个问题?我在 chrome.
上使用 grease monkey
基本上,使用纯 XPath 1.0 实现您所解释的内容的模式可能如下所示:
expression1 | expression2[not(expression1)]
所以,在你的问题的上下文中:
- expression1(截断):
//div[@class=\'entry-content\']/p[...]
- 表达式 2:
//h3[contains(.,\'Share this\')]
解决方法:
//div[@class=\'entry-content\']/p[...] |
//h3[contains(.,\'Share this\')]
[not(//div[@class=\'entry-content\']/p[...])]
如果在 Javascript 的帮助下实施,这可能会更清晰。直接执行expression1,如果结果长度为0则执行expression2。
所以我在 chrome、autopagerizer 上添加了一个用户脚本。
目前我正在使用
//div[@class=\'entry-content\']/p[contains(.,\'chapter\') or contains(.,\'Chapter\') or preceding-sibling::p[contains(.,\'Chapter\')] and (following-sibling::p[contains(.,\'Translator Ramblings\')] or following-sibling::p[contains(.,\'Translating Ramblings\')] or following-sibling::p[contains(.,\'Ramblings\')] or following-sibling::p[starts-with(.,\'Translator\')] or following-sibling::p[starts-with(.,\'Translating\')])]
我想要一个条件参数,当上面的代码没有解决任何问题时,它会 select
//h3[contains(.,\'Share this\')]
相反。在这种情况下,我只希望 h3 被 selected。我不知道如何在 xpath 中写这个,我已经查找了其他相关问题,但他们没有解决这个问题。如果我不能用 xpath 解决这个问题,我将如何用 javascript 解决这个问题?我在 chrome.
上使用 grease monkey基本上,使用纯 XPath 1.0 实现您所解释的内容的模式可能如下所示:
expression1 | expression2[not(expression1)]
所以,在你的问题的上下文中:
- expression1(截断):
//div[@class=\'entry-content\']/p[...]
- 表达式 2:
//h3[contains(.,\'Share this\')]
解决方法:
//div[@class=\'entry-content\']/p[...] | //h3[contains(.,\'Share this\')] [not(//div[@class=\'entry-content\']/p[...])]
如果在 Javascript 的帮助下实施,这可能会更清晰。直接执行expression1,如果结果长度为0则执行expression2。