CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-htmlunit--htmlunit

A headless browser for Java programs that provides web automation, form handling, JavaScript execution, and DOM manipulation capabilities.

Pending
Overview
Eval results
Files

forms.mddocs/

Form Handling

Comprehensive form interaction capabilities including input field manipulation, form submission, and support for all HTML form element types (text, password, checkbox, radio, select, textarea).

Capabilities

Form Access and Submission

Core form functionality for finding forms and submitting them with or without specific submit elements.

public class HtmlForm extends HtmlElement {
    /**
     * Submit the form using the default submit method
     * @return the Page that loads as a result of form submission
     * @throws IOException if submission fails
     */
    public <P extends Page> P submit() throws IOException;
    
    /**
     * Submit the form using a specific submit element
     * @param submitElement the button or input element to use for submission
     * @return the Page that loads as a result of form submission  
     * @throws IOException if submission fails
     */
    public <P extends Page> P submit(SubmittableElement submitElement) throws IOException;
    
    /**
     * Get a form input element by its name attribute
     * @param name the name attribute value
     * @return HtmlElement with matching name, or null if not found
     */
    public HtmlElement getInputByName(String name);
    
    /**
     * Get all form input elements with the specified name
     * @param name the name attribute value
     * @return List of HtmlElement objects with matching names
     */
    public List<HtmlElement> getInputsByName(String name);
    
    /**
     * Get a form input element by its value attribute
     * @param value the value attribute to search for
     * @return HtmlElement with matching value, or null if not found
     */
    public HtmlElement getInputByValue(String value);
    
    /**
     * Get a textarea element by its name attribute
     * @param name the name attribute value
     * @return HtmlTextArea with matching name, or null if not found
     */
    public HtmlTextArea getTextAreaByName(String name);
    
    /**
     * Get a select element by its name attribute
     * @param name the name attribute value
     * @return HtmlSelect with matching name, or null if not found
     */
    public HtmlSelect getSelectByName(String name);
    
    /**
     * Get a button element by its name attribute
     * @param name the name attribute value
     * @return HtmlButton with matching name, or null if not found
     */
    public HtmlButton getButtonByName(String name);
    
    /**
     * Get the form's method attribute (GET or POST)
     * @return the HTTP method for form submission
     */
    public String getMethodAttribute();
    
    /**
     * Get the form's action attribute (submission URL)
     * @return the URL where the form will be submitted
     */
    public String getActionAttribute();
    
    /**
     * Get the form's encoding type attribute
     * @return the encoding type (e.g., "application/x-www-form-urlencoded")
     */
    public String getEnctypeAttribute();
}

Usage Examples:

HtmlPage page = webClient.getPage("https://example.com/login");
HtmlForm loginForm = page.getFormByName("loginForm");

// Get form information
String method = loginForm.getMethodAttribute(); // "POST"
String action = loginForm.getActionAttribute();  // "/authenticate"

// Find form elements
HtmlTextInput username = (HtmlTextInput) loginForm.getInputByName("username");
HtmlPasswordInput password = (HtmlPasswordInput) loginForm.getInputByName("password");
HtmlSubmitInput submitBtn = (HtmlSubmitInput) loginForm.getInputByValue("Log In");

// Fill and submit form
username.setValue("myuser");
password.setValue("mypass");
HtmlPage result = loginForm.submit(submitBtn);

// Or submit without specific button
HtmlPage result2 = loginForm.submit();

Input Element Base Class

Base functionality for all form input elements including value access and state management.

public abstract class HtmlInput extends HtmlElement {
    /**
     * Get the input's current value
     * @return the current value of the input
     */
    public String getValue();
    
    /**
     * Set the input's value
     * @param value the new value to set
     */
    public void setValue(String value);
    
    /**
     * Get the input's name attribute
     * @return the name attribute value
     */
    public String getName();
    
    /**
     * Get the input's type attribute
     * @return the input type (e.g., "text", "password", "checkbox")
     */
    public String getType();
    
    /**
     * Check if the input is checked (for checkboxes and radio buttons)
     * @return true if the input is checked
     */
    public boolean isChecked();
    
    /**
     * Set the checked state (for checkboxes and radio buttons)
     * @param checked true to check, false to uncheck
     */
    public void setChecked(boolean checked);
    
    /**
     * Check if the input is read-only
     * @return true if the input is read-only
     */
    public boolean isReadOnly();
    
    /**
     * Check if the input is disabled
     * @return true if the input is disabled
     */
    public boolean isDisabled();
}

Text Input Fields

Handle text input fields with typing simulation and text selection.

public class HtmlTextInput extends HtmlInput {
    /**
     * Type text into the field (simulates keyboard input)
     * @param text the text to type
     * @throws IOException if typing fails
     */
    public void type(String text) throws IOException;
    
    /**
     * Select all text in the field
     */
    public void select();
    
    /**
     * Get the maximum length allowed for this input
     * @return the maxlength attribute value, or -1 if not set
     */
    public int getMaxLength();
    
    /**
     * Get the placeholder text
     * @return the placeholder attribute value
     */
    public String getPlaceholder();
}

public class HtmlPasswordInput extends HtmlInput {
    // Inherits all HtmlInput methods
    // Values are handled securely but API is identical
}

Usage Examples:

HtmlTextInput nameField = (HtmlTextInput) form.getInputByName("fullName");
HtmlPasswordInput passField = (HtmlPasswordInput) form.getInputByName("password");

// Set values directly
nameField.setValue("John Doe");
passField.setValue("secretpass");

// Or simulate typing
nameField.type("John Doe");

// Text field specific methods
nameField.select(); // Select all text
int maxLen = nameField.getMaxLength();
String placeholder = nameField.getPlaceholder();

Checkbox and Radio Button Inputs

Handle boolean input controls with checked state management.

public class HtmlCheckBoxInput extends HtmlInput {
    // Inherits all HtmlInput methods
    // Primary interaction through isChecked() and setChecked()
}

public class HtmlRadioButtonInput extends HtmlInput {  
    // Inherits all HtmlInput methods
    // Setting one radio button in a group unchecks others automatically
}

Usage Examples:

// Checkbox handling
HtmlCheckBoxInput newsletter = (HtmlCheckBoxInput) form.getInputByName("newsletter");
newsletter.setChecked(true); // Check the box
boolean isChecked = newsletter.isChecked();

// Radio button handling  
List<HtmlElement> genderOptions = form.getInputsByName("gender");
for (HtmlElement element : genderOptions) {
    if (element instanceof HtmlRadioButtonInput) {
        HtmlRadioButtonInput radio = (HtmlRadioButtonInput) element;
        if ("female".equals(radio.getValue())) {
            radio.setChecked(true); // This unchecks other radios in the group
        }
    }
}

Submit and Button Elements

Handle form submission controls and general button elements.

public class HtmlSubmitInput extends HtmlInput {
    // Inherits all HtmlInput methods
    // Can be used with form.submit(submitElement)
}

public class HtmlButton extends HtmlElement {
    // Inherits all HtmlElement methods including click()
    // Can contain complex content unlike input elements
}

Usage Examples:

// Submit input
HtmlSubmitInput submitBtn = (HtmlSubmitInput) form.getInputByValue("Submit");
HtmlPage result = form.submit(submitBtn);

// Button element
HtmlButton cancelBtn = (HtmlButton) form.getButtonByName("cancel");
Page result2 = cancelBtn.click();

Textarea Elements

Handle multi-line text input areas with content manipulation.

public class HtmlTextArea extends HtmlElement {
    /**
     * Get the text content of the textarea
     * @return the current text content
     */
    public String getText();
    
    /**
     * Set the text content of the textarea
     * @param text the new text content
     */
    public void setText(String text);
    
    /**
     * Type text into the textarea (simulates keyboard input)
     * @param text the text to type
     * @throws IOException if typing fails
     */
    public void type(String text) throws IOException;
    
    /**
     * Get the number of rows
     * @return the rows attribute value
     */
    public int getRows();
    
    /**
     * Get the number of columns
     * @return the cols attribute value
     */
    public int getColumns();
}

Usage Examples:

HtmlTextArea comments = form.getTextAreaByName("comments");

// Set content
comments.setText("This is my feedback about the product...");

// Or simulate typing
comments.type("Additional comments here");

// Get textarea properties
String currentText = comments.getText();
int rows = comments.getRows();
int cols = comments.getColumns();

Select Dropdown Elements

Handle dropdown select elements with option selection and multiple selection support.

public class HtmlSelect extends HtmlElement {
    /**
     * Get all option elements in the select
     * @return List of HtmlOption objects
     */
    public List<HtmlOption> getOptions();
    
    /**
     * Get a specific option by index
     * @param index the option index (0-based)
     * @return HtmlOption at the specified index
     */
    public HtmlOption getOption(int index);
    
    /**
     * Get all currently selected options
     * @return List of selected HtmlOption objects
     */
    public List<HtmlOption> getSelectedOptions();
    
    /**
     * Select an option by index
     * @param index the index of the option to select
     */
    public void setSelectedIndex(int index);
    
    /**
     * Check if this is a multiple selection dropdown
     * @return true if multiple selections are allowed
     */
    public boolean isMultiple();
}

public class HtmlOption extends HtmlElement {
    /**
     * Check if this option is currently selected
     * @return true if the option is selected
     */
    public boolean isSelected();
    
    /**
     * Set the selection state of this option
     * @param selected true to select, false to deselect
     */
    public void setSelected(boolean selected);
    
    /**
     * Get the option's value attribute
     * @return the value that will be submitted if this option is selected
     */
    public String getValue();
    
    /**
     * Get the option's display text
     * @return the text content displayed to the user
     */
    public String getText();
}

Usage Examples:

HtmlSelect countrySelect = form.getSelectByName("country");

// Select by index
countrySelect.setSelectedIndex(2); // Select third option

// Select by finding specific option
List<HtmlOption> options = countrySelect.getOptions();
for (HtmlOption option : options) {
    if ("US".equals(option.getValue())) {
        option.setSelected(true);
        break;
    }
}

// Get selected values
List<HtmlOption> selected = countrySelect.getSelectedOptions();
for (HtmlOption option : selected) {
    System.out.println("Selected: " + option.getText() + " (" + option.getValue() + ")");
}

// Check if multiple selection is allowed
boolean allowsMultiple = countrySelect.isMultiple();

Form Encoding Types

Constants and utilities for form encoding types.

public class FormEncodingType {
    public static final FormEncodingType URL_ENCODED;
    public static final FormEncodingType MULTIPART;
    public static final FormEncodingType TEXT_PLAIN;
    
    /**
     * Get the encoding type name
     * @return the MIME type string for this encoding
     */
    public String getName();
    
    /**
     * Get an encoding type instance by name
     * @param name the encoding type name
     * @return FormEncodingType instance
     */
    public static FormEncodingType getInstance(String name);
}

Usage Examples:

// Check form encoding type
String enctype = form.getEnctypeAttribute();
FormEncodingType encodingType = FormEncodingType.getInstance(enctype);

// Common encoding types
FormEncodingType urlEncoded = FormEncodingType.URL_ENCODED; // application/x-www-form-urlencoded
FormEncodingType multipart = FormEncodingType.MULTIPART;    // multipart/form-data
FormEncodingType textPlain = FormEncodingType.TEXT_PLAIN;   // text/plain

Install with Tessl CLI

npx tessl i tessl/maven-org-htmlunit--htmlunit

docs

cookies.md

exceptions.md

forms.md

http.md

index.md

javascript.md

page-dom.md

web-client.md

windows.md

tile.json