A headless browser intended for use in testing web-based applications.
—
Core browser automation functionality for loading pages, managing browser instances, configuring options, and handling basic navigation. This is the foundation for all HtmlUnit operations.
The main entry point for all HtmlUnit browser automation operations.
/**
* Main browser automation class that simulates a web browser
*/
public class WebClient implements AutoCloseable {
/** Create WebClient with default Chrome browser */
public WebClient();
/** Create WebClient with specific browser version */
public WebClient(BrowserVersion browserVersion);
/** Create WebClient with browser version and proxy settings */
public WebClient(BrowserVersion browserVersion, String proxyHost, int proxyPort);
/** Load page from URL string */
public <P extends Page> P getPage(String url) throws IOException, FailingHttpStatusCodeException;
/** Load page from URL object */
public <P extends Page> P getPage(URL url) throws IOException, FailingHttpStatusCodeException;
/** Load page from custom WebRequest */
public <P extends Page> P getPage(WebRequest request) throws IOException, FailingHttpStatusCodeException;
/** Load page into specific window */
public <P extends Page> P getPage(WebWindow webWindow, WebRequest webRequest) throws IOException, FailingHttpStatusCodeException;
/** Load WebResponse into specific window */
public Page loadWebResponseInto(WebResponse webResponse, WebWindow webWindow) throws IOException;
/** Get current browser configuration options */
public WebClientOptions getOptions();
/** Get browser version being simulated */
public BrowserVersion getBrowserVersion();
/** Get current active window */
public WebWindow getCurrentWindow();
/** Set current active window */
public void setCurrentWindow(WebWindow window);
/** Open new window with URL */
public WebWindow openWindow(URL url, String windowName) throws IOException;
/** Open modal dialog window */
public DialogWindow openDialogWindow(URL url, WebWindow opener, Object dialogArguments) throws IOException;
/** Find window by name */
public WebWindow getWebWindowByName(String name);
/** Add default request header for all requests */
public void addRequestHeader(String name, String value);
/** Remove default request header */
public void removeRequestHeader(String name);
/** Get HTTP connection handler */
public WebConnection getWebConnection();
/** Set custom HTTP connection handler */
public void setWebConnection(WebConnection webConnection);
/** Get cookie manager */
public CookieManager getCookieManager();
/** Set custom cookie manager */
public void setCookieManager(CookieManager cookieManager);
/** Set credentials provider for authentication */
public void setCredentialsProvider(CredentialsProvider credentialsProvider);
/** Set JavaScript alert handler */
public void setAlertHandler(AlertHandler alertHandler);
/** Get JavaScript alert handler */
public AlertHandler getAlertHandler();
/** Set JavaScript confirm handler */
public void setConfirmHandler(ConfirmHandler confirmHandler);
/** Get JavaScript confirm handler */
public ConfirmHandler getConfirmHandler();
/** Set JavaScript prompt handler */
public void setPromptHandler(PromptHandler promptHandler);
/** Get JavaScript prompt handler */
public PromptHandler getPromptHandler();
/** Set JavaScript error listener */
public void setJavaScriptErrorListener(JavaScriptErrorListener javaScriptErrorListener);
/** Get JavaScript error listener */
public JavaScriptErrorListener getJavaScriptErrorListener();
/** Close and cleanup all resources */
public void close();
/** Wait for background JavaScript jobs to complete */
public int waitForBackgroundJavaScript(long timeoutMillis);
/** Wait for background JavaScript jobs with timeout */
public int waitForBackgroundJavaScriptStartingBefore(long delayMillis);
}Usage Examples:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
// Basic browser automation
try (WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage("https://example.com");
String title = page.getTitleText();
System.out.println("Page title: " + title);
}
// Browser with specific version
try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX)) {
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
HtmlPage page = webClient.getPage("https://example.com");
// Work with page...
}
// Browser with proxy
try (WebClient webClient = new WebClient(BrowserVersion.CHROME, "proxy.example.com", 8080)) {
webClient.addRequestHeader("User-Agent", "Custom Bot 1.0");
HtmlPage page = webClient.getPage("https://example.com");
// Work with page...
}Configuration options for customizing WebClient behavior.
/**
* Configuration options for WebClient behavior
*/
public class WebClientOptions {
/** Check if JavaScript execution is enabled */
public boolean isJavaScriptEnabled();
/** Enable or disable JavaScript execution */
public void setJavaScriptEnabled(boolean enabled);
/** Check if CSS processing is enabled */
public boolean isCssEnabled();
/** Enable or disable CSS processing */
public void setCssEnabled(boolean enabled);
/** Check if exceptions are thrown on JavaScript errors */
public boolean isThrowExceptionOnScriptError();
/** Set whether to throw exceptions on JavaScript errors */
public void setThrowExceptionOnScriptError(boolean throwExceptionOnScriptError);
/** Check if failing HTTP status codes throw exceptions */
public boolean isThrowExceptionOnFailingStatusCode();
/** Set whether to throw exceptions on failing HTTP status codes */
public void setThrowExceptionOnFailingStatusCode(boolean throwExceptionOnFailingStatusCode);
/** Check if images are downloaded */
public boolean isDownloadImages();
/** Set whether to download images */
public void setDownloadImages(boolean downloadImages);
/** Get connection timeout in milliseconds */
public int getTimeout();
/** Set connection timeout in milliseconds */
public void setTimeout(int timeout);
/** Check if HTTP redirects are followed */
public boolean isRedirectEnabled();
/** Set whether to follow HTTP redirects */
public void setRedirectEnabled(boolean enabled);
/** Check if insecure SSL connections are allowed */
public boolean isUseInsecureSSL();
/** Set whether to allow insecure SSL connections */
public void setUseInsecureSSL(boolean useInsecureSSL);
/** Get proxy configuration */
public ProxyConfig getProxyConfig();
/** Set proxy configuration */
public void setProxyConfig(ProxyConfig proxyConfig);
/** Check if applets are enabled */
public boolean isAppletEnabled();
/** Set whether applets are enabled */
public void setAppletEnabled(boolean enabled);
/** Check if ActiveX objects are enabled */
public boolean isActiveXNative();
/** Set whether ActiveX objects are enabled */
public void setActiveXNative(boolean enabled);
/** Get maximum redirects allowed */
public int getMaxInMemory();
/** Set maximum redirects allowed */
public void setMaxInMemory(int maxInMemory);
/** Get screen dimensions */
public Dimension getScreenSize();
/** Set screen dimensions */
public void setScreenSize(Dimension screenSize);
}Browser version configuration and feature detection.
/**
* Represents a specific browser version with its capabilities
*/
public class BrowserVersion {
/** Google Chrome (latest version) */
public static final BrowserVersion CHROME;
/** Mozilla Firefox (latest version) */
public static final BrowserVersion FIREFOX;
/** Mozilla Firefox ESR (Extended Support Release) */
public static final BrowserVersion FIREFOX_ESR;
/** Microsoft Edge */
public static final BrowserVersion EDGE;
/** Internet Explorer 11 */
public static final BrowserVersion INTERNET_EXPLORER;
/** Best supported browser (currently Chrome) */
public static final BrowserVersion BEST_SUPPORTED;
/** Get browser application name */
public String getApplicationName();
/** Get browser version string */
public String getApplicationVersion();
/** Get User-Agent string */
public String getUserAgent();
/** Check if browser supports specific feature */
public boolean hasFeature(BrowserFeature feature);
/** Get browser major version number */
public float getBrowserVersionNumeric();
/** Check if this is Internet Explorer */
public boolean isIE();
/** Check if this is Firefox */
public boolean isFirefox();
/** Check if this is Chrome */
public boolean isChrome();
/** Check if this is Edge */
public boolean isEdge();
}Usage Examples:
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
// Configure for specific browser
try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX)) {
// Configure options
WebClientOptions options = webClient.getOptions();
options.setJavaScriptEnabled(true);
options.setCssEnabled(false);
options.setTimeout(30000); // 30 seconds
options.setThrowExceptionOnScriptError(false);
options.setThrowExceptionOnFailingStatusCode(false);
// Load page
HtmlPage page = webClient.getPage("https://example.com");
// Browser-specific behavior
BrowserVersion browser = webClient.getBrowserVersion();
if (browser.isFirefox()) {
System.out.println("Running in Firefox mode");
System.out.println("User Agent: " + browser.getUserAgent());
}
}Base interface for all page types returned by WebClient.
/**
* Base interface for all page types
*/
public interface Page {
/** Get the HTTP response that created this page */
public WebResponse getWebResponse();
/** Get the window containing this page */
public WebWindow getEnclosingWindow();
/** Get the URL of this page */
public URL getUrl();
/** Initialize page after loading */
public void initialize();
/** Clean up page resources */
public void cleanUp();
/** Check if page supports JavaScript */
public boolean isHtmlPage();
}/**
* HTML page with full DOM support
*/
public class HtmlPage extends SgmlPage {
// See HTML DOM Manipulation documentation for full API
}
/**
* Plain text page
*/
public class TextPage extends AbstractPage {
/** Get text content of the page */
public String getContent();
}
/**
* Page with unexpected content type
*/
public class UnexpectedPage extends AbstractPage {
/** Get input stream for page content */
public InputStream getInputStream() throws IOException;
/** Get page content as bytes */
public byte[] getBytes();
}
/**
* XML page with DOM support
*/
public class XmlPage extends SgmlPage {
/** Get XML content as string */
public String asXml();
/** Get XML document */
public Document getXmlDocument();
}/**
* Common target window names
*/
public static final String TARGET_BLANK = "_blank";
public static final String TARGET_SELF = "_self";
public static final String TARGET_PARENT = "_parent";
public static final String TARGET_TOP = "_top";
/**
* HTTP status codes
*/
public static final int OK = 200;
public static final int NOT_FOUND = 404;
public static final int FORBIDDEN = 403;
public static final int INTERNAL_SERVER_ERROR = 500;Install with Tessl CLI
npx tessl i tessl/maven-net-sourceforge-htmlunit--htmlunit