Examples‎ > ‎

Popup window's HTML content

The sample shows how to receive HTML of a currently opened popup window.

In this sample the HeadlessBrowser component is used. So, you are not be able to see any UI components. The result of this sample execution will be a printed in console HTML content of an opened popup window.
import com.jniwrapper.win32.ie.HeadlessBrowser;
import com.jniwrapper.win32.ie.WebBrowser;
import com.jniwrapper.win32.ie.dom.HTMLElement;
import com.jniwrapper.win32.ie.event.NavigationEventAdapter;
import com.jniwrapper.win32.ie.event.NewWindowEventHandler;
import com.jniwrapper.win32.ie.scripting.Robot;
 
/**
 * The sample shows how to receive HTML of a currently opened popup window.
 * <p/>
 * In this sample the HeadlessBrowser component is used. So, you are not
 * be able to see any UI components. The result of this sample execution
 * will be a printed in console HTML content of an opened popup window.
 */
public class GetPopupHtmlSample {
    public static void main(String[] args) throws Exception {
        WebBrowser browser = new HeadlessBrowser();
        // registering Navigation listener to receive the event when
        // the document is loaded completely
        browser.addNavigationListener(new NavigationEventAdapter() {
            public void entireDocumentCompleted(WebBrowser webBrowser, String url) {
                // finding the required link by its text
                Robot robot = new Robot(webBrowser);
                HTMLElement link = robot.findLinkByText("Open the JavaScript Window Example 1");
                // saving default NewWindowHandler to use it later
                final NewWindowEventHandler defaultHandler = webBrowser.getNewWindowHandler();
                // registering our NewWindowHandler to receive the event
                // when a new popup window is going to be shown
                webBrowser.setNewWindowHandler(new NewWindowEventHandler() {
                    public NewWindowAction newWindow() {
                        NewWindowAction action = defaultHandler.newWindow();
                        WebBrowser browser = action.getBrowser();
                        // registering Navigation listener for a popup window to
                        // receive the event when the popup's document is loaded completely
                        browser.addNavigationListener(new NavigationEventAdapter() {
                            public void entireDocumentCompleted(WebBrowser webBrowser, String url) {
                                // printing HTML content of a popup window
                                System.out.println("HTML = " + webBrowser.getContent());
                            }
                        });
                        return action;
                    }
                });
                // clicking on the required link
                if (link != null) {
                    link.click();
                }
            }
        });
        browser.navigate("http://www.javascript-coder.com/window-popup/javascript-window-open-example1.html");
    }
}