2

My webpage contains several <frame>. And I need to find the frame which contains //a[@id='edit'].

So I tried //frame[//a[@id='edit']] but this xpath return 0 element.

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

Any idea?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Royce
  • 1,557
  • 5
  • 19
  • 44
  • 1
    To complete zx485's answer, adding a "." to the request should be enough `//frame[.//a[@id='edit']]`. – E.Wiest Mar 21 '20 at 18:46

2 Answers2

1

You can use the descendant-or-self:: axis like this:

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

The result is the second <frame> element.

zx485
  • 28,498
  • 28
  • 50
  • 59
1

An i.e. inline frame is a construct which embeds a document into an HTML document so that embedded data is displayed inside a subwindow of the browser's window. This does not mean full inclusion and the two documents are independent, and both them are treated as complete documents, instead of treating one as part of the other. Hence you can't identify an <iframe> element through it's descendants.

You can find a detailed discussion in Ways to deal with #document under iframe


Solution

As a solution you have to construct a Locator Strategies based on the <iframe> attributes.

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352