1

I ran into a little issue today while validating some input fields in our application. Looks like we can't call the selenium java method clear() to clear the input and I know I can iterate over the string and use sendKeys(Keys.Backspace) but clear method should do the work?

Anyone else ran into the same issue and how did you work around this problem.?

Our app is built upon Ant libraries

Selenium

String hotmarketNameEdit = "ant-input";

    WebElement edit = driver.findElement(By.className(hotmarketNameEdit));
    edit.click();
    edit.clear();

HTML

<input type="text" id="label" class="ant-input ant-input-sm" value="AUTOMATION TEST - Thu Oct 21 09:54:15 MDT 2021" xpath="1">

Thanks for any inputs

Ahmet Emre Kilinc
  • 5,489
  • 12
  • 30
  • 42

3 Answers3

0

I've personally come across this issue quite a few times, that Selenium .clear() method does not work properly.

Selenium community is aware of this issue, please see the bug reported here - ClearText does not work

You can go through the above-mentioned link.

Workaround :

Majority of time, I try to simulate Ctrl + A to select the text and just press delete after that.

If You are using Java as a binding language, You can do the following

webElement.sendKeys(Keys.CONTROL + "a")
webElement.sendKeys(Keys.DELETE)
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Thanks for your input , I went down that route too :) and it partially worked , basically selected parts of the string visible and the rest left out. lol. It's weird. – Johan Lindstrom Oct 21 '21 at 18:32
0

have you tried JavaScriptExecutor?

JavascriptExecutor jse = (JavascriptExecutor) indexWebDriver;
jse.executeScript("arguments[0].value=''",edit);

check this one also

How can I consistently remove the default text from an input element with Selenium?

Rob
  • 32
  • 4
0

Can you please try this work around, passing both selecting and deleting keywords in one command

driver.findElement(By.name("username")).sendKeys(Keys.chord(Keys.CONTROL,"a", Keys.DELETE));
Jayanth Bala
  • 758
  • 1
  • 5
  • 11