Dynamic Xpath and Passing Variable Into the Xpath

Hi everyone,

I was wondering if someone could help me out with how to make a dynamic Xpath. I need to click on a button that has an xpath that changes depending on the “case number” The following is the xpath:

//*[@id=“checkCases_No_4499”]/td[9]/div[1]/div/div/button/span

I tried to use the following function:

new_btn= WebUI.modifyObjectProperty(findTestObject(‘Object Repository/Page_Designer Dashboard/span_Assign to me’), ‘xpath’, ‘equals’, ‘//tr[@id=“checkCases_No_4500”]/td[9]/div[1]/div/div/button/span’, false)

However, with this I still need to manually change the case number every time I run it.

How do I store the case number as a variable and pass it in the xpath?

Thanks so much! :slight_smile:

Hello,

where does ‘case number’ come from?

It gets automatically generated once a case is sent to our site. It appears on the left side of the screen

The button that I want to click is on the right side. This button has an xpath that is dependent on the case number.

What about this?

String caseNumber = WebUI.getText(findTestObject('Object/With/CaseNumber'))

And then

new_btn= WebUI.modifyObjectProperty(findTestObject(‘Object Repository/Page_Designer Dashboard/span_Assign to me’), ‘xpath’, ‘equals’, ‘//tr[@id=“checkCases_No_' + caseNumber + '”]/td[9]/div[1]/div/div/button/span’, false)

1 Like

I tried doing that but once I do WebUI.click(new_btn) the test fails.

Also, the ‘Object/With/CaseNumber’ also has an xpath that depends on that case number itself.

I tried manually putting String caseNumber = ‘4499’ and passing it to through the modifyObjectProperty function. While it says that I was successfully able to change the property, the test still fails when I click on the button.

Here is the error message:

Thanks so much for the help!

Can you share HTML code of the table row with case number? Also, full exception would be helpful.

Hi @Marek_Melocik,

Is this what you were asking?

4492

image

image

Thanks so much!

image

And this? Is this what you were asking?
Thanks!

Exception says element not interactable - is it displayed correctly at the time you test it? And I may need more HTML code, to make sure your xpath is correct.

What does element not interactable mean? Thanks!

Hi @Marek_Melocik

This is the xpath that Katalon was able to detect using spyweb. When I run it with this xpath (without modifying anything), it works.

image

I modified the xpath using this:
String xpath_assign = ‘//tr[@id="checkCases_No_’ + caseNumber + ‘"]/td[9]/div[1]/div/div/button’

TestObject AssignButton = new TestObject(‘AssignButton’).addProperty(“xpath_assign”, ConditionType.EQUALS, xpath_assign)

WebUI.delay(5)

WebUI.click(AssignButton)

While it says that I am able to change it properly, the xpath generated is:

image

It has double quotation marks around the ID but I do not know how to make it single quotation marks. Could that be why?

↑ This is not correct (property name). ↑

TestObject AssignButton = new TestObject(‘AssignButton’).addProperty(“xpath”, ConditionType.EQUALS, xpath_assign)

And no, it should be the same if you use single or double quotes.

Yeah it works - thanks so much :slight_smile:

please help me on query,

my x-path is : WebUI.click(findTestObject(‘Object Repository/Admin/AcceptButton’))

Object repo, AcceptButton : //*[contains(text(),‘krish’)]/following::div[1]/button[text()=‘Accept’]

I am storing ‘krish’ in varaible: ProgramName,

So that am giving this variable name in xpath like this,

//*[contains(text(),’+ProgramName+’)]/following::div[1]/button[text()=‘Accept’]

but its not clicking, pls help me on this,

Advance Thanks,
:smile:

Hello,

this article may help you

2 Likes

Thanks for quick response,

Its working as i expected

hi, please help me

//button[@data-pika-day = 'startDay' and @data-pika-month = 'startMonth']

im trying to put the variable in xpath but its not working

Caused by: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/a_Universal/start’ located by ‘By.xpath: //button[@data-pika-day = 'startDay' and @data-pika-month = 'startMonth']’ not found

image

1 Like

This worked for me thank you sooooo much Marek