javahtmlunithtmlunit-driver

How to access embedded iframe elements with htmlunit driver?


I want to access the inner content of an embedded iframe with com.gargoylesoftware.htmlunit.WebClient:

<html>
<body>
<iframe...>
    #document
    <html>
    <body>
        ...
        <input name="myinput" />
    </body
</iframe>
</body>
</html>

I can already grab the iframe with:

HtmlInlineFrame iframe = (HtmlInlineFrame) page.getElementsByTagName("iframe").get(0);

Now I want to grab the input element. But even listing any input elements shows only an empty list:

NodeList inputs = iframe.getElementsByTagName("input");

So what might be wrong here? How can I access the inner of the embedded iframe?


Solution

  • Try

    HtmlInlineFrame iframe = (HtmlInlineFrame) page.getElementsByTagName("iframe").get(0);
    HtmlPage innerPage = (HtmlPage) iframe.getEnclosedPage();
    NodeList inputs = innerPage.getElementsByTagName("input");