Xpath 获取上面的元素 [英] Xpath get element above

查看:34
本文介绍了Xpath 获取上面的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的结构:

<div class="b"><span>文本示例</span>

在 xpath 中,我想检索属性attribute"的值,因为里面有文本:Text Example

如果我使用这个 xpath:

.//*[@class='a']///*[text()='Text Example']

它返回的是元素span,但是我需要div.a,因为我需要通过Selenium WebDriver获取属性的值

解决方案

嘿,有很多方法可以解决.

所以假设给出了 Text Example,您可以使用此文本识别它:-

//span[text()='Text Example']/../.. -->如果你知道它的 2 级

//span[text()='Text Example']/ancestor::div[@class='a'] -->如果你不知道这个`div`有多少级

Above 2 xpaths 如果您只想使用 Text Example 标识元素,如果您不想遍历此文本,则可以使用 xpaths.有一些简单的方法可以直接识别:-

//div[@class='a']

suppose I have this structure:

<div class="a" attribute="foo">
    <div class="b">
        <span>Text Example</span>
    </div>
</div>

In xpath, I would like to retrieve the value of the attribute "attribute" given I have the text inside: Text Example

If I use this xpath:

.//*[@class='a']//*[text()='Text Example']

It returns the element span, but I need the div.a, because I need to get the value of the attribute through Selenium WebDriver

解决方案

Hey there are lot of ways by which you can figure it out.

So lets say Text Example is given, you can identify it using this text:-

//span[text()='Text Example']/../..         --> If you know its 2 level up

OR

//span[text()='Text Example']/ancestor::div[@class='a']  --> If you don't know how many level up this `div` is

Above 2 xpaths can be used if you only want to identify the element using Text Example, if you don't want to iterate through this text. There are simple ways to identify it directly:-

//div[@class='a']

这篇关于Xpath 获取上面的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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