Javascript to send text to a field with xpath

I Have a field to which Im unable to sendKeys or setText so i tried using Java script Executor . But I’m unable to pass the Test object as param

Custom keyword
@keyword
def sendKeysJS(TestObject to, String input) {
WebElement element = WebUiCommonHelper.findWebElement(findTestObject(to)
WebDriver driver = DriverFactory.getWebDriver()
String jScript = document.getElementById(element).value = input
JavascriptExecutor executor = (JavascriptExecutor)driver
executor.executeScript(jScript)
}

Called:
CustomKeywords.‘customPackage.Utils.sendKeysJS’(findTestObject(‘CreatePIObject/AssignPI/Reg’), ‘abc’)

Error: Caused by: groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.testobject.ObjectRepository.findTestObject() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject) values: [TestObject - ‘Object Repository/CreatePIObject/AssignPI/Reg’]
Possible solutions: findTestObject(java.lang.String), findTestObject(java.lang.String, java.util.Map)

Can somebody help me with setting text to a field with xpath (Unable to set the text using "SetText "as it doesn’t enter the complete text

Try this and see how it works for you

import com.kms.katalon.core.webui.common.WebUiCommonHelper
 
WebElement element = WebUiCommonHelper.findWebElement(findTestObject('your/object'),30)
WebUI.executeJavaScript("arguments[0].value='Your Value'", Arrays.asList(element))

This works! @hpulsford THANKS

1 Like

@hpulsford Now I have another issue with the same field…I used the above to input the value but the entered value soon disappears when i perform the next action .
I have an input field to which I’m entering the text using the above suggested but the next step is to click on search . The filed gets cleared before it goes to the click step. Any help here ?

Sounds like an issue with your application to me, if you do the exact same thing manually (doing it yourself) do you experience the same behaviour. As in enter the text and then immediately click the search button.

Does the text disappear when search is clicked? or does it disappear after a certain amount of time?

No,This does not happen when I manually enter the text . The text stays there even after the search results are displayed .

No idea why that could be happening, Could you share the html of the element

The "Registration Number is the filed to which I’m trying to enter the Text .

hmm not sure, might be best to raise a new topic with the correct title therefore others will be able to help you out. You can include all the information above just a fresh topic as this is a diffrerent issue :slight_smile:

I have am having the same issue. I am using a date picker. So, my scenario is the text field which accepts date picker value is a readonly text filed and on selecting a date it updates the value of a hidden text field. But when i am automating it, it is setting the value but is clearing it out immediately.

Please follow below tutorial

Thank you so much :slight_smile:

@gauthamkarthik534 , what tutoriar? I cannot see any tutorial neither below nor above