A headless browser intended for use in testing web-based applications.
—
Comprehensive form automation capabilities including field input, selection handling, file uploads, and form submission. Essential for login automation, data entry workflows, and web application testing.
Primary interface for form manipulation and submission.
/**
* HTML form element with submission and field management
*/
public class HtmlForm extends HtmlElement {
/** Submit form using default submit button */
public <P extends Page> P submit() throws IOException;
/** Submit form using specific submit element */
public <P extends Page> P submit(SubmittableElement submitElement) throws IOException;
/** Reset form to default values */
public void reset();
/** Get form method (GET/POST) */
public String getMethodAttribute();
/** Get form action URL */
public String getActionAttribute();
/** Get form encoding type */
public String getEnctypeAttribute();
/** Get form target */
public String getTargetAttribute();
/** Get input element by name */
public HtmlElement getInputByName(String name);
/** Get all input elements by name */
public List<HtmlElement> getInputsByName(String name);
/** Get text input by value */
public HtmlTextInput getInputByValue(String value);
/** Get elements by tag name within form */
public List<HtmlElement> getHtmlElementsByTagName(String tagName);
/** Get select element by name */
public HtmlSelect getSelectByName(String name);
/** Get all select elements by name */
public List<HtmlSelect> getSelectsByName(String name);
/** Get textarea by name */
public HtmlTextArea getTextAreaByName(String name);
/** Get button by name */
public HtmlButton getButtonByName(String name);
/** Check if form accepts file uploads */
public boolean isMultiPartForm();
/** Get form's character set */
public String getAcceptCharsetAttribute();
/** Check if form validation is disabled */
public boolean isNoValidate();
}Usage Examples:
import com.gargoylesoftware.htmlunit.html.*;
try (WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage("https://example.com/login");
// Find and fill login form
HtmlForm loginForm = page.getFormByName("loginForm");
HtmlTextInput usernameField = loginForm.getInputByName("username");
usernameField.setValueAttribute("john.doe");
HtmlPasswordInput passwordField = (HtmlPasswordInput) loginForm.getInputByName("password");
passwordField.setValueAttribute("secretpassword");
HtmlCheckBoxInput rememberMe = (HtmlCheckBoxInput) loginForm.getInputByName("remember");
rememberMe.setChecked(true);
// Submit form
HtmlSubmitInput submitButton = (HtmlSubmitInput) loginForm.getInputByName("submit");
HtmlPage resultPage = submitButton.click();
// Or submit directly
HtmlPage resultPage2 = loginForm.submit();
}Base class for all HTML input elements.
/**
* Base class for HTML input elements
*/
public abstract class HtmlInput extends HtmlElement implements SubmittableElement, DisabledElement {
/** Get input type attribute */
public String getTypeAttribute();
/** Get input value */
public String getValueAttribute();
/** Set input value */
public void setValueAttribute(String value);
/** Get input name */
public String getNameAttribute();
/** Get default value */
public String getDefaultValue();
/** Set default value */
public void setDefaultValue(String defaultValue);
/** Check if input is disabled */
public boolean isDisabled();
/** Set disabled state */
public void setDisabled(boolean disabled);
/** Check if input is read-only */
public boolean isReadOnly();
/** Set read-only state */
public void setReadOnly(boolean readOnly);
/** Check if input is required */
public boolean isRequired();
/** Get input size attribute */
public String getSizeAttribute();
/** Get maxlength attribute */
public String getMaxLengthAttribute();
/** Get placeholder text */
public String getPlaceholderAttribute();
/** Check if input has focus */
public boolean hasFocus();
/** Select all text in input */
public void select();
/** Get form containing this input (if any) */
public HtmlForm getEnclosingForm();
/** Check if input value is valid */
public boolean isValid();
/** Get validation message */
public String getValidationMessage();
/** Check validity and show validation message */
public boolean checkValidity();
/** Set custom validation message */
public void setCustomValidity(String message);
}/**
* Single-line text input (<input type="text">)
*/
public class HtmlTextInput extends HtmlInput {
/** Type text into field (fires events) */
public void type(String text) throws IOException;
/** Append text to existing value */
public void appendText(String text) throws IOException;
/** Clear the input field */
public void clear();
/** Get text selection start position */
public int getSelectionStart();
/** Get text selection end position */
public int getSelectionEnd();
/** Set text selection range */
public void setSelectionRange(int start, int end);
}
/**
* Password input (<input type="password">)
*/
public class HtmlPasswordInput extends HtmlInput {
/** Type password text (fires events) */
public void type(String text) throws IOException;
/** Clear password field */
public void clear();
}
/**
* Hidden input (<input type="hidden">)
*/
public class HtmlHiddenInput extends HtmlInput {
// Hidden inputs have same base functionality as HtmlInput
}
/**
* Email input (<input type="email">)
*/
public class HtmlEmailInput extends HtmlInput {
/** Type email text (fires events) */
public void type(String text) throws IOException;
/** Get list attribute for datalist */
public String getListAttribute();
}
/**
* Number input (<input type="number">)
*/
public class HtmlNumberInput extends HtmlInput {
/** Get min value */
public String getMinAttribute();
/** Get max value */
public String getMaxAttribute();
/** Get step value */
public String getStepAttribute();
/** Step up by specified amount */
public void stepUp(int n);
/** Step down by specified amount */
public void stepDown(int n);
}
/**
* URL input (<input type="url">)
*/
public class HtmlUrlInput extends HtmlInput {
/** Type URL text (fires events) */
public void type(String text) throws IOException;
}
/**
* Search input (<input type="search">)
*/
public class HtmlSearchInput extends HtmlInput {
/** Type search text (fires events) */
public void type(String text) throws IOException;
}
/**
* Telephone input (<input type="tel">)
*/
public class HtmlTelephoneInput extends HtmlInput {
/** Type telephone text (fires events) */
public void type(String text) throws IOException;
}/**
* Submit button (<input type="submit">)
*/
public class HtmlSubmitInput extends HtmlInput {
/** Click to submit form */
public <P extends Page> P click() throws IOException;
/** Get form value sent when this button submits */
public String getValueAttribute();
}
/**
* Generic button (<input type="button">)
*/
public class HtmlButtonInput extends HtmlInput {
/** Click button (fires events) */
public <P extends Page> P click() throws IOException;
}
/**
* Reset button (<input type="reset">)
*/
public class HtmlResetInput extends HtmlInput {
/** Click to reset form */
public <P extends Page> P click() throws IOException;
}
/**
* Image button (<input type="image">)
*/
public class HtmlImageInput extends HtmlInput {
/** Click image button */
public <P extends Page> P click() throws IOException;
/** Click at specific coordinates */
public <P extends Page> P click(int x, int y) throws IOException;
/** Get image source URL */
public String getSrcAttribute();
/** Get image alt text */
public String getAltAttribute();
}
/**
* Button element (<button>)
*/
public class HtmlButton extends HtmlElement implements SubmittableElement, DisabledElement {
/** Click button */
public <P extends Page> P click() throws IOException;
/** Get button type (submit/button/reset) */
public String getTypeAttribute();
/** Get button name */
public String getNameAttribute();
/** Get button value */
public String getValueAttribute();
/** Check if disabled */
public boolean isDisabled();
/** Set disabled state */
public void setDisabled(boolean disabled);
/** Get form containing button */
public HtmlForm getEnclosingForm();
}/**
* Checkbox input (<input type="checkbox">)
*/
public class HtmlCheckBoxInput extends HtmlInput {
/** Check if checkbox is checked */
public boolean isChecked();
/** Set checked state */
public void setChecked(boolean checked);
/** Get checkbox value when checked */
public String getValueAttribute();
/** Check if checkbox is indeterminate */
public boolean isIndeterminate();
/** Set indeterminate state */
public void setIndeterminate(boolean indeterminate);
/** Click to toggle checked state */
public <P extends Page> P click() throws IOException;
}
/**
* Radio button (<input type="radio">)
*/
public class HtmlRadioButtonInput extends HtmlInput {
/** Check if radio button is selected */
public boolean isChecked();
/** Set selected state (unselects others in group) */
public void setChecked(boolean checked);
/** Get radio button value */
public String getValueAttribute();
/** Click to select this radio button */
public <P extends Page> P click() throws IOException;
/** Get other radio buttons in same group */
public List<HtmlRadioButtonInput> getRadioGroup();
}/**
* File upload input (<input type="file">)
*/
public class HtmlFileInput extends HtmlInput {
/** Set files to upload */
public void setFiles(File... files);
/** Set file data directly */
public void setData(byte[] data);
/** Set file with specific filename and content type */
public void setContentType(String contentType);
/** Get uploaded files */
public File[] getFiles();
/** Check if multiple files allowed */
public boolean isMultiple();
/** Get accepted file types */
public String getAcceptAttribute();
/** Clear selected files */
public void clearFiles();
}Usage Examples:
// File upload
HtmlFileInput fileInput = (HtmlFileInput) form.getInputByName("attachment");
File uploadFile = new File("/path/to/document.pdf");
fileInput.setFiles(uploadFile);
// Multiple file upload
File[] files = {
new File("/path/file1.jpg"),
new File("/path/file2.png")
};
fileInput.setFiles(files);
// Direct data upload
byte[] fileData = "file content".getBytes();
fileInput.setData(fileData);
fileInput.setContentType("text/plain");/**
* Select dropdown (<select>)
*/
public class HtmlSelect extends HtmlElement implements SubmittableElement, DisabledElement {
/** Get all option elements */
public List<HtmlOption> getOptions();
/** Get currently selected options */
public List<HtmlOption> getSelectedOptions();
/** Get option by value attribute */
public HtmlOption getOptionByValue(String value);
/** Get option by display text */
public HtmlOption getOptionByText(String text);
/** Get option at index */
public HtmlOption getOption(int index);
/** Set option selected by value */
public void setSelectedAttribute(String value, boolean selected);
/** Set option selected by index */
public void setSelectedIndex(int index);
/** Check if multiple selection is enabled */
public boolean isMultipleSelectEnabled();
/** Get select name */
public String getNameAttribute();
/** Get select size (visible options) */
public String getSizeAttribute();
/** Check if disabled */
public boolean isDisabled();
/** Set disabled state */
public void setDisabled(boolean disabled);
/** Check if required */
public boolean isRequired();
/** Get form containing select */
public HtmlForm getEnclosingForm();
/** Clear all selections */
public void clearSelection();
}
/**
* Option element (<option>)
*/
public class HtmlOption extends HtmlElement {
/** Check if option is selected */
public boolean isSelected();
/** Set selected state */
public void setSelected(boolean selected);
/** Get option value */
public String getValueAttribute();
/** Get option display text */
public String getText();
/** Check if option is disabled */
public boolean isDisabled();
/** Set disabled state */
public void setDisabled(boolean disabled);
/** Get option index in select */
public int getIndex();
/** Click to select option */
public <P extends Page> P click() throws IOException;
}
/**
* Option group (<optgroup>)
*/
public class HtmlOptionGroup extends HtmlElement {
/** Get group label */
public String getLabel();
/** Check if group is disabled */
public boolean isDisabled();
/** Get options in this group */
public List<HtmlOption> getOptions();
}Usage Examples:
// Select dropdown manipulation
HtmlSelect countrySelect = form.getSelectByName("country");
// Select by value
countrySelect.setSelectedAttribute("US", true);
// Select by text
HtmlOption canadaOption = countrySelect.getOptionByText("Canada");
canadaOption.setSelected(true);
// Multiple selection
HtmlSelect skillsSelect = form.getSelectByName("skills");
if (skillsSelect.isMultipleSelectEnabled()) {
skillsSelect.setSelectedAttribute("java", true);
skillsSelect.setSelectedAttribute("python", true);
skillsSelect.setSelectedAttribute("javascript", true);
}
// Get selected values
List<HtmlOption> selected = countrySelect.getSelectedOptions();
for (HtmlOption option : selected) {
System.out.println("Selected: " + option.getValueAttribute());
}/**
* Multi-line text area (<textarea>)
*/
public class HtmlTextArea extends HtmlElement implements SubmittableElement, DisabledElement {
/** Get textarea text content */
public String getText();
/** Set textarea text content */
public void setText(String text);
/** Get textarea name */
public String getNameAttribute();
/** Get number of rows */
public int getRows();
/** Get number of columns */
public int getCols();
/** Check if disabled */
public boolean isDisabled();
/** Set disabled state */
public void setDisabled(boolean disabled);
/** Check if read-only */
public boolean isReadOnly();
/** Set read-only state */
public void setReadOnly(boolean readOnly);
/** Check if required */
public boolean isRequired();
/** Get maximum length */
public String getMaxLengthAttribute();
/** Get placeholder text */
public String getPlaceholderAttribute();
/** Type text (fires events) */
public void type(String text) throws IOException;
/** Select all text */
public void select();
/** Get form containing textarea */
public HtmlForm getEnclosingForm();
/** Get text selection start */
public int getSelectionStart();
/** Get text selection end */
public int getSelectionEnd();
/** Set selection range */
public void setSelectionRange(int start, int end);
}/**
* Date input (<input type="date">)
*/
public class HtmlDateInput extends HtmlInput {
/** Get min date */
public String getMinAttribute();
/** Get max date */
public String getMaxAttribute();
/** Get step attribute */
public String getStepAttribute();
/** Step up by specified amount */
public void stepUp(int n);
/** Step down by specified amount */
public void stepDown(int n);
}
/**
* Time input (<input type="time">)
*/
public class HtmlTimeInput extends HtmlInput {
/** Get min time */
public String getMinAttribute();
/** Get max time */
public String getMaxAttribute();
/** Get step attribute */
public String getStepAttribute();
/** Step up by specified amount */
public void stepUp(int n);
/** Step down by specified amount */
public void stepDown(int n);
}
/**
* DateTime-local input (<input type="datetime-local">)
*/
public class HtmlDateTimeLocalInput extends HtmlInput {
/** Get min datetime */
public String getMinAttribute();
/** Get max datetime */
public String getMaxAttribute();
/** Get step attribute */
public String getStepAttribute();
/** Step up by specified amount */
public void stepUp(int n);
/** Step down by specified amount */
public void stepDown(int n);
}/**
* Interface for elements that can be submitted with forms
*/
public interface SubmittableElement {
/** Get element name for form submission */
String getName();
/** Get element value for form submission */
String getValue();
/** Set default value */
void setDefaultValue(String defaultValue);
/** Reset to default value */
void reset();
}
/**
* Interface for elements that can be disabled
*/
public interface DisabledElement {
/** Check if element is disabled */
boolean isDisabled();
/** Set disabled state */
void setDisabled(boolean disabled);
}
/**
* Interface for form validation
*/
public interface ValidatableElement {
/** Check if element value is valid */
boolean isValid();
/** Get validation message */
String getValidationMessage();
/** Check validity and show message */
boolean checkValidity();
/** Set custom validation message */
void setCustomValidity(String message);
}/**
* Form encoding type constants
*/
public class FormEncodingType {
/** URL encoded form data (default) */
public static final FormEncodingType URL_ENCODED = new FormEncodingType("application/x-www-form-urlencoded");
/** Multipart form data (for file uploads) */
public static final FormEncodingType MULTIPART = new FormEncodingType("multipart/form-data");
/** Plain text encoding */
public static final FormEncodingType TEXT_PLAIN = new FormEncodingType("text/plain");
/** Get encoding type name */
public String getName();
}Install with Tessl CLI
npx tessl i tessl/maven-net-sourceforge-htmlunit--htmlunit