Enter text into fields that contain validation

Many input fields that contain validation do not allow text to be "pushed" into them and instead expect letters to be entered one at a time. You can use JavaScript to achieve this, as shown in the following Java sample code. The sample code uses a driver and a WebElement to enter text into the input field and executes the validation through a dispatch event.

Copy

Java code example

 public static void javaScriptSendkeys(RemoteWebDriver driver, WebElement webElementTextBox, String text) {
        String jsScript = "let input = arguments[0]; \r\n" + "let lastValue = input.value;\r\n"                + "input.value = arguments[1];\r\n" + "let event = new Event('input', { bubbles: true });\r\n"                + "event.simulated = true;\r\n"                + "let tracker = input._valueTracker;\r\n" + "if (tracker) {\r\n" + "  tracker.setValue(lastValue);\r\n"                + "}\r\n" + "input.dispatchEvent(event);";
        driver.executeScript(jsScript, webElementTextBox, text);
    }
}