getattribute xpath


#1

Hi all,
i am newbie in using Katalon and facing an issue while trying to get XPath from web object. Below, a part of the code.
In fact, XPath of the object contains a numeric value i need to extract to work on another element of the page “(//div[@id=‘plats-36427’]/dd/div)” where 36427 is the numéric value i want to extract.

TestObject to_Plate = new TestObject(‘objectName_Plate’)
to_Plate.addProperty(‘tag’, ConditionType.EQUALS, ‘div’)
to_Plate.addProperty(‘class’, ConditionType.EQUALS, ‘input-box’)
to_Plate.addProperty(‘text’, ConditionType.CONTAINS, plate)

sProperty = WebUI.getAttribute(to_Plate, “xpath”)

Tks in advance pour your help.
Arnaud


#2

Extracting that number is probably not very difficult. But I’m confused as to why you need to extract it. Is the XPath built and parameterized in some way?

//Obtain you xpath however you normally do it
String xpath = "(//div[@id='plats-36427']/dd/div)"

xpath = xpath.split(/plats-/)[1]  // ==> 36427']/dd/div)
String num = xpath.split(/\'/)[0]  // ==> 36427
println "num is " + num

#3

Dear @Russ_Thomas, many thanks for your help. I am confused because i did not detailed correctly my need.

The picture bellow should help to understand, i hope.

Based on the first line, i can get the data-id-categorie (36640 in this example).
So, i can identify div but i am not able to get the list of dd contained in the div (sample code bellow)

The goal is to update the quantity value based on the 2 input parameters “Plats groupe” and “1/4 poulet…”.

TestObject to_dtEntity = new TestObject(‘objectName_dtEntity’)
to_dtEntity.addProperty(‘tag’, ConditionType.EQUALS, ‘dt’)
to_dtEntity.addProperty(‘text’, ConditionType.CONTAINS, entity)
to_dtEntity.addProperty(‘class’, ConditionType.STARTS_WITH, ‘choix-plat menu-required’)

def sclass = WebUI.getAttribute(to_dtEntity, ‘class’)
if (sclass.contains(‘is–open’) == false) {
WebUI.click(to_dtEntity)
}

def sDataID = WebUI.getAttribute(to_dtEntity, ‘data-id-categorie’)
println("$sDataID")
==>this code works fine and i get the correct number

TestObject to = new TestObject(‘objectName_to’)
to.addProperty(“xpath”, ConditionType.CONTAINS, “//div[@id='plats-” + sDataID +"’]/" )

‘Find WebElements based on above match condition’
ArrayList wes = WebUiCommonHelper.findWebElements(to, 10)
==>But this code returns no element

Thanks in advance for your help.
Arnaud


#4

You have the following¥:

I doubt it. The combination of “xpath” and ConditionType.CONTAINS does not make sense.

Please try the following:

TestObject to = new TestObject('objectName_to')
to.addProperty("xpath", ConditionType.EQUALS, "//div[@id='plats-${sDataID}']" )


#5

Hi again,
i am afraid that there is a concept i am not able to understand.

For example, the first peace of code works fine. I am able to interact and get attribute using WebUI.
But in other cases, this does not work.

TestObject to_Plate = new TestObject(‘objectName_Plate’)
to_Plate.addProperty(‘tag’, ConditionType.EQUALS, ‘dd’)
to_Plate.addProperty(‘text’, ConditionType.CONTAINS, plate)
//to_Plate.addProperty(‘xpath’, ConditionType.EQUALS, scond)

WebUI.doubleClick(to_Plate)

def sText = WebUI.getAttribute(to_Plate, ‘text’)
def sattsecond = WebUI.getAttribute(to_Plate, ‘xpath’)
def sProperty = to_Plate.findPropertyValue(‘xpath’)

The WebUI double click works fine, but when i want to get text properties, the output are empty …

2019-11-29 18:09:15.271 DEBUG testcase.User_select_Plate_Quatity - 9: scond = “//div[@id='plats-” + sDataID + “’]/dd[1]”
2019-11-29 18:09:15.271 DEBUG testcase.User_select_Plate_Quatity - 10: println($scond)
//div[@id=‘plats-36640’]/dd[1]
2019-11-29 18:09:15.271 DEBUG testcase.User_select_Plate_Quatity - 11: to_Plate = new com.kms.katalon.core.testobject.TestObject(objectName_Plate)
2019-11-29 18:09:15.272 DEBUG testcase.User_select_Plate_Quatity - 12: to_Plate.addProperty(“tag”, EQUALS, “dd”)
2019-11-29 18:09:15.272 DEBUG testcase.User_select_Plate_Quatity - 13: to_Plate.addProperty(“text”, CONTAINS, plate)
2019-11-29 18:09:15.272 DEBUG testcase.User_select_Plate_Quatity - 14: doubleClick(to_Plate)
2019-11-29 18:09:15.712 DEBUG testcase.User_select_Plate_Quatity - 15: sText = getAttribute(to_Plate, “text”)
2019-11-29 18:09:15.994 DEBUG testcase.User_select_Plate_Quatity - 16: println($sText)
null
2019-11-29 18:09:16.014 DEBUG testcase.User_select_Plate_Quatity - 17: sattsecond = getAttribute(to_Plate, “xpath”)
2019-11-29 18:09:16.288 DEBUG testcase.User_select_Plate_Quatity - 18: println($sattsecond)
null
2019-11-29 18:09:16.289 DEBUG testcase.User_select_Plate_Quatity - 19: sProperty = to_Plate.findPropertyValue(“xpath”)
2019-11-29 18:09:16.290 DEBUG testcase.User_select_Plate_Quatity - 20: println($sProperty)


#6

You are mixing up the properties of TestObjects with the attributes of HTML elements - they are not the same things.

HTML elements are markup artifacts of a web page, represented by objects in the DOM.

TestObjects are test artifacts (typically stored in the Object Repository) which capture information about HTML elements.

Specifically, you cannot retrieve an xpath attribute from an HTML element <= there is no such thing.


#7

tks ! it helped me …