Example I
Getting the page inside <frame> element or <iframe> element can be done by using
HtmlPage.getFrames().
Suppose you have the following page:
<html> <body> <iframe src="two.html"> </body> </html>
You can use the following code to get the content of two.html
:
final List<FrameWindow> window = page.getFrames(); final HtmlPage pageTwo = (HtmlPage) window.get(0).getEnclosedPage();
Example II
Another example that navigates API docs to get a desired page of a class:
final WebClient client = new WebClient(); final HtmlPage mainPage = client.getPage("https://www.htmlunit.org/apidocs/index.html");
To get the page of the first frame (at upper left) and click the sixth link:
final HtmlPage packageListPage = (HtmlPage) mainPage.getFrames().get(0).getEnclosedPage(); packageListPage.getAnchors().get(5).click();
To get the page of the frame named 'packageFrame' (at lower left) and click the second link:
final HtmlPage packagePage = (HtmlPage) mainPage.getFrameByName("packageFrame").getEnclosedPage(); packagePage.getAnchors().get(1).click();
To get the page of the frame named 'classFrame' (at right):
final HtmlPage classPage = (HtmlPage) mainPage.getFrameByName("classFrame").getEnclosedPage();