[XPath] How to create a XPath locator with a variables

Hello everyone! Thank you for your help in my last post. Today I have another issue again, which is how to create a XPath locator with a variables

First of all, I am still practicing my testing on the Magento website with my last script, which is trying to create add other item, but I have to restructure my add one item script.

I read many forum and doc such as How to handle dynamic xpath?, Use variables in Web test objects Doc but I am still confuse… since I have tried to do the same but still error…

I use xpath as: //a[contains(text(),'{itemName}')]
This is my script:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import internal.GlobalVariable as GlobalVariable




TestObject itemLink = findTestObject('Object Repository/Search_Results_Page/Item_Text')

def itemName = 'Adrienne_Trek_Jacket'

WebUI.modifyObjectProperty(itemLink, 'text', itemName, 'equals', false)

WebUI.waitForElementVisible(itemLink, 5)



I am not sure what I am wrong, is my library not complete…?

This is my result error:

Thank you for your help. :grinning:


You need to add a dollar sign ( $ ) in front of the first curly bracket, so your pathway becomes:

You can probably do it in code like you have above, or you can put this in your test script with a “replacement” for your itemName, like:

WebUI.verifyElementVisible(findTestObject('Search_Results_Page/Item_Text', ["itemName" : 'Adrienne_Trek_Jacket']))

NOTE: watch the placement of parenthesises if you use parameterization like I did.


WebUI.verifyElementVisible(findTestObject('mypage/input_Lines_Item name Index', ["index" : 0]))

Edit: It shouldn’t matter in parameterization, however, in code you need to use double quotes ( " ) surrounding your string if you do any replacement; if you need to, use single quotes ( ’ ) inside to do anything that needs ‘quotes’.