seleniumselenium-webdriverselenium-chromedriver

selenium clear() command doesn't clear the element


I have been writing selenium scripts for a while in Java. I encountered a very weird issue today. Here is the issue:

I cleared a text field using webelement.clear() method, later while executing next command (click event), the text area I had previously cleared, is now populated with previously filled value.

Here is the code snippet:

mobileNumField.get(0).clear();
Thread.sleep(4500);
emailAddress.get(0).click();
emailAddress.get(0).clear();
Thread.sleep(4500);
emailAddress.get(0).sendKeys(Keys.TAB);

Solution

  • Faced a similar problem. The input field is cleared but the error message is not updated. It seems that some input fields work correctly only if you enter and delete a character:

    element.sendKeys(text);
    element.sendKeys(Keys.SPACE, Keys.BACK_SPACE);