XPath:仅基于 text() 获取以下兄弟姐妹 [英] XPath: Get following-sibling based on text() only

查看:27
本文介绍了XPath:仅基于 text() 获取以下兄弟姐妹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用相对XPathSelenium,有没有办法获取following-sibling的文本内容,使用两者的文本内容兄弟姐妹指导?

Using relative XPath with Selenium, is there a way to get the text contents of following-sibling, using the text contents of both siblings as guidance?

<table class="table">
    <tbody>
        <tr>
            <td>Looking for</td>
            <td class="text-right">This!</td>
        </tr>
    </tbody>
</table>

在上面的例子中,我想根据文本Looking for检索文本片段This!.

In the above example, I want to retreive the text snippet This! based on the text Looking for.

路径必须相对,因为这些表格单元格可以出现在页面的任何位置.以上是完整网页的剪裁.也可以在同一个 块之前插入其他内容.

The path needs to be relative in the sense that these table cells can appear anywhere on the page. The above is a cut-out of the full web page. There can also be other things inserted before inside the same <tr> block.

我试过这个似乎找到了第一个单元格:

I have tried this which seems to find the first cell:

//text()[contains(.,'Looking for')]

但是,我正在苦苦思索如何在 2nd 兄弟姐妹中获取文本.

However, I'm struggling with how to proceed to get the text in the 2nd sibling.

我的尝试看起来像(错误地):

My attempt has looked something like (incorrectly):

//text()[contains(.,'Looking for')]/following-sibling::text()

推荐答案

您几乎做对了,开始吧,您只需要显示您想要查找的确切位置.我希望这会有所帮助:

You almost got it right, here you go, you just needed to show where exactly you want to look for. I hope this helps:

//td[text()="Looking for"]/following-sibling::td[text()="This!"]

这篇关于XPath:仅基于 text() 获取以下兄弟姐妹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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