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


#1

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.doubleClick(findTestObject(‘OR/Input_field’))
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.
WebUI.clearText(findTestObject(‘Patient/Page_E-Besok_Patient/Child/Input_Child_SSN’))

//DELETE is not working as well
WebUI.click(findTestObject(‘OR/Input_field’))

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

Also tried the keyword
@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()

	WebUI.delay(1)
	
	rb.keyPress(KeyEvent.VK_BACK_SPACE)
	
	WebUI.delay(1)
	
	rb.keyRelease(KeyEvent.VK_BACK_SPACE)

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


#2

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.


#3

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)


#4

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


#5

Hi @jasmi_shaheer

Did the solution @Russ_Thomas provided works ?


#6

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