Set text javascript is not working properly

My problem is that I find the element in a certain web page where I want to set a text and then I do something like this:
WebElement elementCI = WebUiCommonHelper.findWebElement(findTestObject(‘Object Repository/Page_Ingreso Orden/input_Nmero de Documento_vPERSONADOCUMENTONUMERO’),
30)

WebUI.delay(2)

WebUI.executeJavaScript(‘arguments[0].value=‘47247777’’, Arrays.asList(elementCI))

But the way this is working is that when I enter this page, the text is already in its place and then its deleted. As this happnes, the element I need to wait to appear does not appear. This is not an error you would see in the log. I know something is wrong or missing but I do not know what. Maybe I need to simulate an enter or something, I dont know. Could you please help me?

I’m sorry to have to tell you, but text assignment in JavaScript works fine, 100% of the time.

Your problem must be caused by something else (without seeing more of the code/page it’s hard to suggest what that might be).

Thats the problem, the test works fine and it passes.
In this test case I try to login in to the web and access a page called Carga de Ordenes
Then enter the ID of a person
Wait for something to appear
Enter something else and finish

For some reason when I set text in login I can do it correctly but when I want to enter text else where it does not so I had to do it in a special way using something like this WebUI.executeJavaScript(‘arguments[0].value=‘47247777’’, Arrays.asList(element))
This enters the text but then deletes it right away so after that nothing happens and the text passes correctly

This is the script I have:
WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘url’)

WebUI.setText(findTestObject(‘Page_Login/input_User Name_vUSERNAME’), ‘admin’)

WebUI.setEncryptedText(findTestObject(‘Page_Login/input_User Password_vUSERPASSWORD’), ‘hUKwJTbofgPU9eVlw/CnDQ==’)

WebUI.click(findTestObject(‘Page_Login/input_User Password_LOGIN’))

WebUI.click(findTestObject(‘Page_Inicio/a_Pre Analtica’))

WebUI.delay(2)

WebUI.click(findTestObject(‘Page_Inicio/a_Carga de rdenes’))

WebUI.waitForPageLoad(5)

WebUI.waitForElementVisible(findTestObject(‘Page_Ingreso Orden/input_Nmero de Documento_vPERSONADOCUMENTONUMERO’), 5)

WebElement element = WebUiCommonHelper.findWebElement(findTestObject(‘Object Repository/Page_Ingreso Orden/input_Nmero de Documento_vPERSONADOCUMENTONUMERO’),
30)

WebUI.delay(2)

WebUI.executeJavaScript(‘arguments[0].value=‘47247777’’, Arrays.asList(element))

WebUI.delay(4)

  • Do I need to import something extra? add a special keyword? Do something that I am not doing? Is my script wrong?
    Thanks for the help
    Guillermo

Trust me, if what you say is happening is true, then the world wide web is broken - time to run for the hills.

Since the web is not broken (and since JavaScript itself is not broken) there must be something else going on.

Show me a screen capture of the element in question when you bring it up in the developer tools. If you use Firefox, we can see if there are events attached (which my be causing your problem).

Problem solved:
I cant really know why text was deleted but I solved it by adding a webUI.SendKeys(…Enter…) after the javascript

Thank you very much for the help. If I encounter this problem again (even with my solution) I will tell you
Guillermo

1 Like

Hi Russ, im having the same problem, the value atribute is not changing using WebUI.executescript

more info: as you can see, the javascriptexecutor set the data but not appears on html code.

It may not appear in the value attribute of the DOM. Your code changes the value property.

thanks for your answer Russ, yes, my code sets the date (as you can see on the image) but if i inspect the dom, the value atribute continues empty and when i interact with the next object, this date value dissapier.

You can find out what is stored in the birthdate field in the devtools console:

document.querySelector("#birthDate").value

That will retrieve the value stored the value property.

thnks again Russ, the problem is that i cant use the WebUI.executeJavascript to modify the value attribute. the value is not updating using the method. ill, check some workaround

That’s because that’s not what the code does - it modifies the value PROPERTY. To modify the attribute, you need a different API - but that would be the WRONG thing to do.

thanks Russ, WebUI.executeJavaScript(“arguments[0].value=‘Your Value’”, Arrays.asList(element)) not modifies the dom value ? could you share the correct way if my code its not valid?
The problem is, im using the mobile browser to test a web app (through appium) and i need to avoid interact with the native android datepicker because i cant access to this object through webdriver instance, if i use the .apk , i would not have this problem because with Mobile.api(katalon) , i could access native android objects…is for that reason that i need to set the date using javascript, as i said, with my code, the date is filled but when i click in the next object, this value dissapier.
thanks a lot.

for your info, js.executeScript(“document.getElementById(‘birthDate’).setAttribute(‘value’, ‘1990-01-01’)”); its working, the dom value is filled but the value dissapier when i click in the next object