Search code examples
seleniumtestingsafaridriver

SafariDriver: inputElement.clear() doesn't work as expected


This only happens on Safari with SafariDriver:

For a textinput element, clear() can clear the field indeed, but the following values sent using sendKeys() are actually extended with the original value.

Example:

  1. Start -- input value: 1

  2. input.clear() -- input value: empty

  3. input.sendKeys(2) -- input value: 12

Also tried sendKeys(Key.HOME,Key.chord(Key.SHIFT,Key.END),newVal) to select all and replace, but this simply not working. Nothing was selected.

Any suggestion is appreciated.


Solution

  • Try to click element first and then use clear() method to clear text, it works for me many times may be for you also:

    WebElement myInputElement= driver.findElement("Locator value");
    myInputElement.click();
    myInputElement.clear();
    myInputElement.sendKeys('Testing');