How to get the number of elements

How to get the number of elements,I remember selenium can use find_ elements

You can use the selenium method in KS, like.

List<WebElement> rows_table = Table.findElements(By.tagName('tr'));
numItems = rows_table.size();

or like:

List<WebElement> myList = WebUI.findWebElements(TestObject)
numItems = myList.size();

Also, if you are talking the number of Options in a drop-down list, then there is:

numItems =  WebUI.getNumberOfTotalOption(findTestObject('myPage/select_ProvinceState'));

or the number of selected Options:

numItems = WebUI.getNumberOfSelectedOption(findTestObject('myPage/select_ProvinceState'))

Hi 1968693027,

You can use the Katalon class WebUiCommonHelper… Follow the code below:

import com.kms.katalon.core.webui.common.WebUiCommonHelper
import org.openqa.selenium.WebElement

private void sampleMethod() {
		
		List<WebElement> elems = WebUiCommonHelper.findWebElements(findTestObject("Your_Object"), 30)
		
		println elems[0].getText()
		println elems.text
		println elems.size()
	}

Hope that helps. . . :smiley:

2 Likes

Do you know the reason for the failure
List elems = WebUiCommonHelper.findWebElements(findTestObject(‘Object Repository/Page_IO Controller WAN/Deploy/Underlay_Network/Wan_Interface/text_UnderlayWan_SDIp_Num’),30)

Test Cases/Deploy/Underlay_Network/Wan_Interface/Public/Add_And_Edit_UnderlayWan_Base_Step FAILED.
Reason:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
file:/C:/Users/Administrator/Katalon%20Studio/IO-WAN1.50/Scripts/Deploy/Underlay_Network/Wan_Interface/Public/Add_And_Edit_UnderlayWan_Base_Step/Script1634261573571.groovy: 164: The current scope already contains a variable of the name elems
@ line 164, column 19.
List elems = WebUiCommonHelper.findWebElements(findTestObject(‘Object Repository/Page_IO Controller WAN/Deploy/Underlay_Network/Wan_Interface/text_UnderlayWan_SDIp_Num’),30)
^

1 error

at com.kms.katalon.core.main.ScriptEngine.getScript(ScriptEngine.java:199)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:445)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:436)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:415)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:407)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:284)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.doCall(CallTestCaseKeyword.groovy:59)
at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword$_callTestCase_closure1.call(CallTestCaseKeyword.groovy)
at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:74)
at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.callTestCase(CallTestCaseKeyword.groovy:81)
at com.kms.katalon.core.keyword.builtin.CallTestCaseKeyword.execute(CallTestCaseKeyword.groovy:44)
at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:74)
at com.kms.katalon.core.keyword.BuiltinKeywords.callTestCase(BuiltinKeywords.groovy:334)
at Add_UnderlayWan.run(Add_UnderlayWan:106)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:445)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:436)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:415)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:407)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:284)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1650529991164.run(TempTestCase1650529991164.groovy:25)

The error message states the cause for your MultipleCompilationErrorsException; you have already initiated (declared) a variable and you are doing it again (only declare a variable once within scope). To fix this error, leave the declaration of List, or List<WebElement>, on your first statement, but remove the declaration of List, or List<WebElement>, for the variable elems on all others and do not try to redeclare the same variable as something else, like a number or String.

As an example:


List<WebElement> elems = WebUiCommonHelper.findWebElements(findTestObject('Object Repository/Page_IO Controller WAN/Deploy/Underlay_Network/Wan_Interface/text_UnderlayWan_SDIp_Num'),30)
...
elems = WebUiCommonHelper.findWebElements(findTestObject('Object Repository/Page_IO Controller WAN/Deploy/Underlay_Network/Wan_Interface/text_SevenOaks'),30)
...
elems = WebUiCommonHelper.findWebElements(findTestObject('Object Repository/Page_IO Controller WAN/Deploy/Underlay_Network/Wan_Interface/text_DisneyWorld'),30)

Hi 1968693027,

findWebElements will return list(Array) items and in order to use the web element methods, you should include WebElement like this:

List<WebElement> elems = WebUiCommonHelper.findWebElements(findTestObject(‘Object Repository/Page_IO Controller WAN/Deploy/Underlay_Network/Wan_Interface/text_UnderlayWan_SDIp_Num’), 30)

But if you just need to locate one specific element using selenium approach and utilize the findTestObject of katalon at the same time then you can use this approach:

WebElement elem = WebUiCommonHelper.findWebElement(findTestObject(‘Object Repository/Page_IO Controller WAN/Deploy/Underlay_Network/Wan_Interface/text_UnderlayWan_SDIp_Num’), 30)

findWebElements if you want to get many objects and store it in an array.
findWebElement if you want to locate a specific object.

Hope that helps. . . :smiley: