【问题标题】:How to find the element which contains a specific sub-element?如何找到包含特定子元素的元素?
【发布时间】:2020-03-21 17:49:32
【问题描述】:

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

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

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

有什么想法吗?

【问题讨论】:

  • 要完成zx485的回答,添加一个“。”到请求应该足够了//frame[.//a[@id='edit']].

标签: selenium selenium-webdriver xpath iframe webdriver


【解决方案1】:

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

您可以在Ways to deal with #document under iframe找到详细讨论


解决方案

作为一种解决方案,您必须基于&lt;iframe&gt; 属性构造一个Locator Strategies

你可以在Switch to an iframe through Selenium and python找到几个详细的讨论

【讨论】:

    【解决方案2】:

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

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

    结果是第二个&lt;frame&gt; 元素。

    【讨论】:

    • 谢谢。你知道它是否与硒一起工作?或者如果我必须在之前切换框架?
    • 这是一个 XPath-1.0 解决方案,所以它应该与 Selenium 一起使用。
    猜你喜欢
    • 2016-07-18
    • 2018-06-15
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2016-06-25
    • 1970-01-01
    相关资源
    最近更新 更多