How to handle dynamic xpath?

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.

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++;

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

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

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 !

Hi @ThanhTo ,

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

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.

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

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

Howerver you wrote:

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?

1 Like

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 :slight_smile:

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:

Thank you :slight_smile: @kazurayam.

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

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.

The following post of mine months ago may help:

I also have this kind of problem with handling dynamic xpaths. Here

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

Error Xpath

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

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
}
}
}

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