JxBrowser displays the content of various types, e.g. images, video, PDF, Flash etc, but in most cases you will display the HTML content of a web page. This guide shows how to access the content of a web page, get the currently selected text, find some text on a web page, save the web page as a file or a set of files, etc.

Content Size

By default the Browser size is empty. Many web pages rely on this size and require it not to be empty. Otherwise, the layout of the DOM document might not be parsed and displayed at all.

If you do not need to display the content of a loaded web page, but the page should “think” it is loaded in a Browser with a non-empty size, then please set the size programmatically using the Browser.setSize(Size) method.

Accessing HTML

To get a string that represents HTML of a Frame please use the Frame.getHtml() method.

Please call this method only when the Frame is loaded completely. Otherwise you may receive incomplete HTML or an empty string.

The following example demonstrates how to print HTML of the currently loaded main Frame:

browser.getMainFrame().ifPresent(frame -> System.out.println(frame.getHtml()));

Accessing Selection

To get selection on the currently loaded Frame as HTML use the Frame.getSelectionAsHtml() method:

String selectionAsHtml = frame.getSelectionAsHtml();

If you only need the selected text without the selection’s HTML, please use the Frame.getSelectionAsText() method as described in the example below:

String selectionAsText = frame.getSelectionAsText();

Finding Text

JxBrowser allows you to find text on the currently loaded web page and highlight all the matches.

JxBrowser searches text only through the visible content of the loaded web pages which have non-empty size.

To perform the search on the currently loaded web page use the TextFinder class:

TextFinder textFinder = browser.getTextFinder();

The following example demonstrates how to find the “text” on the currently loaded web page with the specified search parameters, and wait until the search has been completed:

// Find "text" in the currently loaded web page with the given params.
textFinder.find("text", FindParams.newBuilder()
        .build(), findResult -> {
    // Check if the text search has been finished.
    if (findResult.getState() == State.FINISHED) {
        // Text search has been finished, so get total the number of matches.
        int numberOfMatches = findResult.getNumberOfMatches();
    if (findResult.getState() == State.SEARCHING) {
        // The Text Finder is still searching the text.
        // It's recommended to wait until the search is finished.

The search matches are highlighted as follows:

Find Text Highlights

To clear the highlighted search results on a web page and cancel the search please use the TextFinder.stopFinding(StopFindAction) method.

For example:


Saving Web Page

JxBrowser allows you to save a web page as a file or a set of files.

It is done via the Browser.saveWebPage(String, String, SavePageType) method, which accepts three parameters:

  • A string with a name of the target file.
  • A string with the directory path to save page resources.
  • A type of the save operation (SavePageType).

For example:

String filePath = new File("index.html").getAbsolutePath();
String dirPath = new File("resources_dir").getAbsolutePath();
if (browser.saveWebPage(filePath, dirPath, SavePageType.COMPLETE_HTML)) {
    System.out.println("The web page has been saved to " + filePath);
} else {
    System.err.println("Failed to save the web page to " + filePath);
Go Top