WebWindow

All pages are contained within WebWindow objects. This could be a TopLevelWindow representing an actual browser window, an HtmlFrame representing a <frame> element, or an HtmlInlineFrame representing an <iframe> element.

When a WebClient is first instantiated, a TopLevelWindow is automatically created. You can think of this as the first window displayed by a web browser. Calling WebClient.getPage(WebWindow, WebRequest) will load the new page into this window.
Closing the last TopLevelWindow of a WebClient will automatically open a new empty window.

The JavaScript open() function can be used to load pages into other windows. New WebWindow objects will be created automatically by this function.

WebWindowEvents

If you wish to be notified when windows are created or pages are loaded, register a WebWindowListener with the WebClient via WebClient.addWebWindowListener(WebWindowListener).

When a window is opened either by JavaScript or through the WebClient, a WebWindowEvent will be fired and passed into the WebWindowListener.webWindowOpened(WebWindowEvent) method. Note that both the new and old pages in the event will be null because the window does not have any content loaded at this point. If a URL was specified during creation of the window, the page will be loaded and another event will be fired.

When a new page is loaded into a specific window, a WebWindowEvent is fired and passed into the WebWindowListener.webWindowContentChanged(WebWindowEvent) method.

When an existing window is closed, WebWindowListener.webWindowClosed(WebWindowEvent) is fired.

Frame Example I

Getting the page inside a <frame> 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> windows = page.getFrames();
final HtmlPage pageTwo = (HtmlPage) windows.get(0).getEnclosedPage();

Frame Example II

Here is an example that navigates API documentation to retrieve a desired class page:

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 (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' (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' (right):

final HtmlPage classPage = (HtmlPage) mainPage.getFrameByName("classFrame").getEnclosedPage();