How can I get the current input of a textbox on a page without saving the information, e.g if i want to edit something without saving, how can I verify my input String is entered into the textbox

N.B I am not allowed to save the form in this scenario. The problem is getText(); against the box will just return an empty string because this must be essentially the saved value, but I haven't saved yet.

    public boolean checkTextBoxMaxLength(String Textbox, int maxLength) {
    int overMaxLength = maxLength + 1;
        char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < overMaxLength; i++) {
            char c = chars[random.nextInt(chars.length)];
    String output = sb.toString();

    WebElement ele = driver.findElement(By.id(Textbox));

    ele = driver.findElement(By.id(Textbox));
    String maximum = ele.getAttribute("maxlength");
    System.out.println("Element maxlenght is: " +maximum);

    String input = ele.getText();

    if (input.equals(output.substring(0,output.length()-1))) {  
    return true;
} else {
    return false;

In the above scenario, input will always equal " " as nothing has been saved, is there a simple solution to get the text as it currently exists pre-save?

1 Answer 1


Its not returning an empty value because it is not saved, but because the input field does not have a text it has a value instead.

Try element.getAttribute("value")

Also check for alternatives: https://stackoverflow.com/questions/7852287/using-selenium-web-driver-to-retrieve-value-of-a-html-input

  • Hi Niels, I didnt even think to try that as when I inspected the element it had no value attribute associated with it, safe to say it has one by default even if it not listed in the HTML? Also, answer is perfect, thanks!
    – symon
    Mar 10, 2017 at 9:55
  • Yes @symon, value for text field attribute is by default even it its not listed in HTML. Thanks Niels for answer. Mar 10, 2017 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.