I have to set value for div tag

I need to automate a process where the element is of type div. I need to set a value for that field. Below is the code I’m using:

TestObject divElement = findTestObject('Object Repository/CC_1/Page_CPQ Core/input_DP_ag-3284-input');
WebUI.executeJavaScript("arguments[0].innerText = 'DP';", Arrays.asList(WebUI.findWebElement(divElement)));

Although this code sets the value, it resets to the default when moving to the next element. Can someone help me resolve this issue?

1 Like

I don’t see what you mean by this. Please elaborate it with some evidence; logs, screenshots etc.

You say your object is type <div>, yet the name of the object starts with “input_”. So, how about showing us your HTML of this object so we can see what the tag of your object actually is. My bet is on the <input>.

Katalon supplies the “setText” and “sendKeys” methods to put text into a textbox or textarea. Is there a reason you want to use Javascript? Have you tried to use “setText” or “sendKeys”?

I wanted to examine if I can modify the innerText of a HTML element other than <input> such as div, p, and h1 by the code that @chilumukurisreeja200 presented.

I made a runnable Test Case:

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

TestObject makeTestObject(String id, String xpath) {
	TestObject tObj = new TestObject(id)
	tObj.addProperty("xpath", ConditionType.EQUALS, xpath)
	return tObj
}

WebUI.openBrowser("http://demoaut-mimic.kazurayam.com/")

TestObject h1 = makeTestObject("h1", "//h1")

WebUI.executeJavaScript(
	"arguments[0].innerText = 'Guri, Gura';", 
	Arrays.asList(WebUI.findWebElement(h1, 10))
	);

WebUI.delay(3)

String txt = WebUI.getText(h1)

WebUI.comment(txt)

//WebUI.closeBrowser()

When I ran this, I saw the <h1>CURA Healthcare Service</h1> was changed to <h1>Guri, Gura</h1>.

It was an interesting experiment.

What’s interesting?

WebUI.findWebElement(h1, 10) — this Groovy code fragment returns an instance of org.openqa.selenium.WebElement class in the Test Case runtime in Katalon Studio JVM.

On the other hand, arguments[0].innerText = 'Guri, Gura'; — this string is sent from the Katalon Studio into a Browser. Browser interpretes as a JavaScript fragment. Browser successfully located an HTML Element (<h1>) in the DOM, and modified its innter text.

The WebElement object in the Groovy script on KS was somehow transformed into the HTML Element in the DOM in JavaScript inside the browser. A magical interconnection. This transformation is defined by the W3C Recommendation 05 June 2018 WebDriver specification, and was implemented by the browsers and the webdrivers . This experiment gave me a chance to see how WebDriver protocol works.