Get text of multiple div elements

Hello guys!
I would really appreciate some help.

In my test case, I want to get the text of specific elements (names), store them in an array and compare them to an other array.

I tried multiple variations, but I just can’t get the values. I hope someone else could solve this kind of problem in the past and can help to me.

Thanks in advance!

I tried multiple variations,

Please show what you have done. Attaching screenshots would help.

Right now, I can get the first element like this:

def Names = []

Names.push(WebUI.getText(findTestObject(‘MyObject’))

def ArrayLength = Names.size()

println("The length of the array: " + ArrayLength)

The elements got the same xpath (div/class)

Could you show me

(1) HTML code of your target web page, especially elements of your interest. Open the URL with Browser, open the DevTool by F12 key and find the HTML source.

(2) The xpath code of the ‘MyObject’. Its screenshot is required.

It might be a surprise for you but in the built-in keywords of Katalon Studio, there is none which returns the contents of HTML elements as List. You need to develop some custom solution.

I made another post:

I suppose this would be useful for you.

2 Likes

Awesome!
It seems like working, but throws the following error:

[ERROR] - Test Cases/Test1 FAILED because (of) org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object ‘[Name 1, Name 2, Name 3, Name 4, Name 5, Other Name 1, Other Name 2]’ with class ‘java.util.ArrayList’ to class ‘java.awt.List’ due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.awt.List(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)

Any idea?
Unfortunately I can’t upload screenshots of the HTML code, but I really appreciate your help!

I managed to solve it like this:
ArrayList Names = CustomKeywords.‘newkeywordpackage.FindElementsByXPath.getElementContentsAsList’(’//div[@class =" YourClass"]’)

Thanks for the help! I hope this will be useful for others too!

Katalon Studio provides a builtin solution
com.kms.katalon.core.webui.keyword.WebUiBuiltinKeywords#findWebElements(TestObject, int)

https://api-docs.katalon.com/com/kms/katalon/core/webui/keyword/WebUiBuiltInKeywords.html#findWebElements(TestObject,%20int)