# How to handle dynamic xpath?

**URL:** <https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [April 5, 2017, 12:21pm UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240 "2017-04-05T12:21:18Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![Jervin](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@Jervin](https://forum.katalon.com/u/Jervin)\
**Post date:** [August 30, 2018, 6:31pm UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/21 "2018-08-30T18:31:42Z")

</div>

Hi Guys,

Hope this helps.

Try to use the condition Contains or Starts with condition in the properties of the object if the xpath or ID of the object contains at least a dynamic word.

---

<div class="post-metadata">

**Author:** ![Davinder\_Singh](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@Davinder\_Singh](https://forum.katalon.com/u/Davinder_Singh)\
**Post date:** [October 4, 2018, 7:45pm UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/22 "2018-10-04T19:45:26Z")

</div>

what if the element shifts to the next row each time a new element is entered. So I have a + sign and each time a new entry is made it shifts to the next row i am using

String newpath = (’/html/body/my-app/div[2]/div/jobsheet/jobsheet-grid-tablet/div/table/tbody/tr[’+cval+’]/td[1]/span’

println newpath;

WebUI.modifyObjectProperty(findTestObject(‘jobsheet/existing account/ existing’), ‘xpath’, ‘equals’, newpath, true)

cval++;

---

<div class="post-metadata">

**Author:** ![Davinder\_Singh](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@Davinder\_Singh](https://forum.katalon.com/u/Davinder_Singh)\
**Post date:** [October 5, 2018, 5:24pm UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/23 "2018-10-05T17:24:16Z")

</div>

for those of you using a modify object property you can follow this link for a solution that worked for me

> **[modifyObjectProperty](http://forum.katalon.com/discussion/2024/modifyobjectproperty)**
>
> Hi I want to modify a property at run time. I perform the steps that are indicated in the docs: 'Modify xpath of \\'Login\\' button'

---

<div class="post-metadata">

**Author:** ![curious\_cat](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@curious\_cat](https://forum.katalon.com/u/curious_cat)\
**Post date:** [February 20, 2019, 2:33am UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/24 "2019-02-20T02:33:38Z")

</div>

Hi Guys,

I’ve a drop-down list (list of locations).  
My application can access different instances by changing the url. Functionality for each instance remains the same. But, the list of locations for each instance may be different. I’m trying to search for a location say, A-ABC on my first instance and record the testing it works fine when i play it. But when I run the same test case on another instance it fails to find that location, because i the second instance the Location lies in a different list index.

Eg:

1. Xpath for 1st instance-\> //\*[@id=“main”]/div[1]/div[1]/div[3]/div/div/ul/li[7]/a/span[1]
2. Xpath for 2nd instance-\> //\*[@id=“main”]/div[1]/div[1]/div[3]/div/div/ul/li[8]/a/span[1]

So, the li index has changed from 7 to 8 from first instance to second.  
I cannot set a variable because, I do not want to hard code the li index.  
Can someone help me find a solution to run my application without having to hardcode the xpath?. @Naveen @Marek_Melocik @Zarashima @kazurayam

---

<div class="post-metadata">

**Author:** ![ThanhTo](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/thanhto/32/697_2.png) [@ThanhTo](https://forum.katalon.com/u/ThanhTo)\
**Post date:** [February 20, 2019, 4:21am UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/25 "2019-02-20T04:21:55Z")

</div>

Hi @curious_cat

Can you show us a screenshot of your drop-down list items. Also a screenshot of the test object of a particular item would also help because it possibly does contain some XPaths that you can use.

Regards !

---

<div class="post-metadata">

**Author:** ![curious\_cat](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@curious\_cat](https://forum.katalon.com/u/curious_cat)\
**Post date:** [February 20, 2019, 5:52am UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/26 "2019-02-20T05:52:17Z")

</div>

Hi @ThanhTo ,

Thanks for your time and reply.  
Below is the screenshot of the html for the drop down list

 ![Snippet](https://europe1.discourse-cdn.com/katalon/original/2X/1/1f3e234f309de9f5e6425295f6479971488cc454.png)

And, this is the xpath - //\*[@id=“main”]/div[1]/div[1]/div[3]/div/div/ul/li[8]/a/span[1]

Any help or suggestions are much appreciated.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 21, 2019, 12:29am UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/27 "2019-02-21T00:29:49Z")

</div>

> [@curious\_cat](#):
>
> Eg:
> 
> 1. Xpath for 1st instance-\> //\*[@id=“main”]/div[1]/div[1]/div[3]/div/div/ul/li[7]/a/span[1]
> 2. Xpath for 2nd instance-\> //\*[@id=“main”]/div[1]/div[1]/div[3]/div/div/ul/li[8]/a/span[1]
> 
> So, the li index has changed from 7 to 8 from first instance to second.

The following XPath should be able to select the \<li\> element which shows “A-ABC”.

```auto
//*[@id="main"]//ul[@role="menu"]/li[contains(text(), "A-ABC")]

```

Howerver you wrote:

> [@curious\_cat](#):
>
> I’m trying to search for a location say, A-ABC

What do you mean “seaching for a location”? What information do you want to get? Do you want to find the position of the \<li\> of A-ABC in the \<ul\>? Do you want to get the integer 7 or 8? Which Katalon keyword do you use in your test case script?

---

<div class="post-metadata">

**Author:** ![curious\_cat](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@curious\_cat](https://forum.katalon.com/u/curious_cat)\
**Post date:** [February 21, 2019, 11:23pm UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/28 "2019-02-21T23:23:08Z")

</div>

Hi @kazurayam

Katalon Studio by default recorded the position of the li index.  
I want to get the location by it’s name, so that the search is not restricted to the position. I want to make it generalized to get the location by it’s name irrespective of whether the location’s position may/may not shift.

I’ll try the above solution suggested. Thank you 🙂

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 22, 2019, 12:23am UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/29 "2019-02-22T00:23:53Z")

</div>

> [@curious\_cat](#):
>
> Katalon Studio by default recorded the position of the li index.

Katalon Studio provides a few samples of possible XPath expressions, but these would not necessarily be what you want when your requirement is complexed.

Do you want to get the position (7 or 8) of the \<li\> element with ‘A-ABC’ in \<ul\>? It is a problem complexed enough. To tell the true, Katalon Studio’s built-in keywords do not provide short-cutting capability of getting it (7 or 8). You need to write a few lines of Groovy code to achieve it yourself.

I would not explain it here, because you need to learn the XPath technology itself to understand it. I would suggest to you to study XPath by reading some tutorials such as:

- [XPath in Selenium: How to Find & Write? (Text, Contains, AND)](https://www.guru99.com/xpath-selenium.html)
- [position - XPath | MDN](https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/position)

---

<div class="post-metadata">

**Author:** ![curious\_cat](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@curious\_cat](https://forum.katalon.com/u/curious_cat)\
**Post date:** [February 22, 2019, 12:31am UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/30 "2019-02-22T00:31:15Z")

</div>

Thank you 🙂 @kazurayam.

---

<div class="post-metadata">

**Author:** ![johannes.santoso](https://avatars.discourse-cdn.com/v4/letter/j/c5a1d2/32.png) [@johannes.santoso](https://forum.katalon.com/u/johannes.santoso)\
**Post date:** [March 13, 2019, 8:28am UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/31 "2019-03-13T08:28:14Z")

</div>

Hi,

I’m a new member of automation testing, but I’ve managed some of the automation test with Katalon and it really helps me a lot.  
I have a problem with my dynamic Xpath.  
The purpose of my code is to change the checkInDateDay into dynamic value that I have set randomly before.

So, this is part of my code:

> TestObject to1 = findTestObject(‘Guest\_User\_Book\_Hotel\_CreditCard/btn\_date\_19’)  
> println (“Check in date : " + checkInDateDay)  
> to1.findProperty(‘xpath’).setValue('(//button[@class=“pika-button pika-day” and text()=”’ + checkInDateDay + ‘"])[1]’)  
> println to1.getSelectorCollection().get(SelectorMethod.XPATH)  
> WebUI.click(to1)  
> WebUI.delay(1)

This is my output:

> Check in date : 2  
> (//button[@class=“pika-button pika-day” and text()=“19”])[1]

So, the `text()="19"` should be changed to `text()="2"`

Anyone can help me to solve this?

Thank you very much.

Best Regards,

Johannes

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [March 13, 2019, 8:57am UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/32 "2019-03-13T08:57:16Z")

</div>

```auto
to1.findProperty(‘xpath’).setValue(’(//button[@class=“pika-button pika-day” and text()=”’ + checkInDateDay + ‘"])[1]’)

```

Here you expects the TestObject named `to1` is updated to have new XPath value.

I do not think it will work.

The only way I know is to create a new empty TestObject instance on the fly, and addProperty(“xpath”, “expression you want”).

Possiblly you should read the xpath expression string out of the to1, and modify the string as you want (19 -\> 2). Then you create a new TestObject and set the xpath into it.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [March 13, 2019, 9:00am UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/33 "2019-03-13T09:00:05Z")

</div>

The following post of mine months ago may help:

> [@Parameterized Test Object on the fly](http://forum.katalon.com/t/parameterized-test-object-on-the-fly/11542):
>
> I have made a demo here: Problem to solve Let’s imagine you have a target Web page which contains a menu with 20 \<ul\> elements each of which contains 10 times of fragment: \<li\>\<a href=".."\>label\</a\>\</li\>. The way how \<ul\> and \<li\> elements are marked up is uniform. You want to verify the label texts against some fixture data. You will use Katalon Studio to do the test. Katalon’s documents would suggest to you to create a set of Test Objects in the Object Repository using the Spy Object. Howev…

---

<div class="post-metadata">

**Author:** ![rushantha](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/rushantha/32/5777_2.png) [@rushantha](https://forum.katalon.com/u/rushantha)\
**Post date:** [June 5, 2019, 5:53am UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/34 "2019-06-05T05:53:03Z")

</div>

I also have this kind of problem with handling dynamic xpaths. [Here](http://forum.katalon.com/t/dynamic-xpath-handling-in-web-testing/27029?u=rushantha)

---

<div class="post-metadata">

**Author:** ![pragyakhushirai](https://avatars.discourse-cdn.com/v4/letter/p/e9c0ed/32.png) [@pragyakhushirai](https://forum.katalon.com/u/pragyakhushirai)\
**Post date:** [July 19, 2019, 9:26am UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/35 "2019-07-19T09:26:31Z")

</div>

Good Day Everyone,

I want to perform the same action.. In my aplication I have two hyperlink Refer “ApplicationUI”. So I have Customised my xpath using text() but I am getting error.Please refer screenshot

 ![Attribute](https://europe1.discourse-cdn.com/katalon/original/2X/9/904ab2075de763cad6b702b31070a12bd16ce657.png) ![Error](https://europe1.discourse-cdn.com/katalon/original/2X/d/d191f5e13395bf38e10161a9001a2b9cfb8ccae2.png) ![Variable](https://europe1.discourse-cdn.com/katalon/original/2X/9/9560e3bfc8167e668192f3b47c46d343d54bcf5c.png) ![Xpath](https://europe1.discourse-cdn.com/katalon/original/2X/5/50b8263824d90a1ad3461d6f74f8a6cc08a3f658.png)

---

<div class="post-metadata">

**Author:** ![kgprakash](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@kgprakash](https://forum.katalon.com/u/kgprakash)\
**Post date:** [August 12, 2019, 8:40pm UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/36 "2019-08-12T20:40:24Z")

</div>

I have a table as below and the data will be generated from the database.

 ![Screenshot_1](https://europe1.discourse-cdn.com/katalon/original/2X/4/40c0d569a2a5f8989850a51284da0897544f0e7b.png)

I want to click on the Effective date in the table and will be navigated to a new page. written the code as below but saying unable to locate the table itself. please help me  
String ExpectedValue = ‘07012019’

WebDriver driver = DriverFactory.getWebDriver()

‘To locate table’

WebElement Table = driver.findElement(By.xpath(“//table[@id=‘tableENF123Submissions’]/tbody/tr/td/a”))  
WebUI.waitForElementPresent(ExpectedValue, 30)  
'To locate rows of table it will Capture all the rows available in the table ’

List Rows = Table.findElements(By.tagName(‘tr’))

println('No. of rows: ’ + Rows.size())

‘Find a matching text in a table and performing action’

‘Loop will execute for all the rows of the table’

table: for (int i = 0; i \< Rows.size(); i++) {

‘To locate columns(cells) of that specific row’

List Cols = Rows.get(i).findElements(By.tagName(‘td’))

for (int j = 0; j \< Cols.size(); j++) {

‘Verifying the expected text in the each cell’

if (Cols.get(j).getText().equalsIgnoreCase(ExpectedValue)) {

‘To locate anchor in the expected value matched row to perform action’

Cols.get(1).findElement(By.tagName(‘a’)).click()

table: break  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![salman.ahmad](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/salman.ahmad/32/17516_2.png) [@salman.ahmad](https://forum.katalon.com/u/salman.ahmad)\
**Post date:** [August 11, 2021, 7:47am UTC](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240/37 "2021-08-11T07:47:35Z")

</div>

Your provided solution worked for me. Thank you so much.

[Previous page](https://forum.katalon.com/t/how-to-handle-dynamic-xpath/6240.md?page=1)
