如何找到包含特定子元素的元素?

How to find the element which contains a specific sub-element?

我的网页包含几个<frame>。我需要找到包含 //a[@id='edit'].

的框架

所以我尝试了 //frame[//a[@id='edit']] 但是这个 xpath return 0 元素。

<frame>
    ...
</frame>
<frame>
    #document
    <html>
        <head></head>
        <body>
            ...
            <a id='edit'>Edit</a>   <!-- Added closing tag -->
            ...
        </body>
    </html>
</frame>
<frame>
    ...
</frame>

有什么想法吗?

您可以像这样使用 descendant-or-self:: 轴:

//frame[descendant-or-self::a[@id='edit']]

结果是第二个 <frame> 元素。

即内联框架是一种将文档嵌入到 HTML 文档中的构造,以便嵌入的数据显示在浏览器 [=24= 的子 window 中].这并不意味着完全包含,并且两个文件是独立的,并且它们都被视为完整的文件,而不是将一个文件视为另一个文件的一部分。因此,您无法通过其后代来识别 <iframe> 元素。

You can find a detailed discussion in


解决方案

作为解决方案,您必须根据 <iframe> 属性构建

You can find a couple of detailed discussions in Switch to an iframe through Selenium and python