Deleting text of an input field, Solution that works in all the browsers

Hi there,

I have been trying to delete(clear) text in an input field. But I could not find a solution which works on all browsers. I am using MacBook air. So far I tried all the below

// This introduces weird symbols in Firefox
WebUI.sendKeys(findTestObject(‘OR/Input_field’), Keys.chord(Keys.BACK_SPACE))

//set text empty string work in Firefox not in chrome

WebUI.setText(findTestObject(‘OR/Input_field’), ’ ')

//Clear text not working in chrome, it just appends next string to the previous one without erasing it.

//DELETE is not working as well‘OR/Input_field’))

WebUI.WebUI.sendKeys(findTestObject(‘OR/Input_field’), Keys.DELETE)

Also tried the keyword
def static clearElementText(TestObject to) {
WebElement element = WebUiCommonHelper.findWebElement(to,GlobalVariable.Timeout)
WebUI.executeJavaScript(“arguments[0].value=‘’”, Arrays.asList(element))

Tried the key events

Robot rb = new Robot()


Is there any other solution that works on all browsers and machines?

Have you tried to pass Cntrl+A then just set the text?

WebUI.sendKeys(findTestObject('txt_Comment'), Keys.chord(Keys.CONTROL, 'a'))
WebUI.setText(findTestObject('Your Object'), Text)

Edit - the complete error logs of when it doesnt work would be great, to try and figure out exactly whats happening.

1 Like

This should work everywhere. If not, the internet is broken, the world is coming to an end, find a cave and stay there :confused:

String js = 'document.querySelector("[name=my_input]").value = "";'
WebUI.executeJavaScript(js, null)

If the input has an id attribute:

String js = 'document.querySelector("#my_input").value = "";'
WebUI.executeJavaScript(js, null)

Good luck. (but you shouldn’t need it)

OK. Thank you. I will try these and let you know :slight_smile:

Hi @jasmi_shaheer

Did the solution @Russ_Thomas provided works ?

Hi, I haven’t been able to test that solution yet. But I will test and will definitely update here :). Thank you.

I want to delete a password text present in input box,I have tried with ClearText and SetText/SetMaskText/SetEncryptedText among all of them are not working in windows Chrome.


I have tried with the sendKeys to delete and backspace as well.but still not happening