Dynamic Xpath and Passing Variable Into the Xpath


#1

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:


#2

Hello,

where does ‘case number’ come from?


#3

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.


#4

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)


#5

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!


#6

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


#7

Hi @Marek_Melocik,

Is this what you were asking?

4492

image

image

Thanks so much!


#8

image

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


#9

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.


#10

What does element not interactable mean? Thanks!


#11

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?


#12

↑ 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.


#13

Yeah it works - thanks so much :slight_smile:


#14

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:


#15

Hello,

this article may help you


#16

Thanks for quick response,

Its working as i expected