How to find xpath for element using another dynamic element

In the below scenario i have an element with dynamic value 1251408 and i have to select the option from the dropdown element towards its left i.e i have to select the user corresponding to element 1251408 only…
What needs to be done in this case???

The element with value 1251408 is not clickable though.

You will need to share the HTML of that part of the page if you want an answer.

Attached herewith…

html.zip (198.6 KB)

You can use xpath selector '//td[contains(.,"1251408")]//preceding-sibling::td/select'.

Next time, you can just copy and paste a relevant part of the HTML. This would suffice:

Thanks Mate, but i am storing the value 1251408 in a variable as this value is dynamic and going to change always, so in that case what would be my xpath selector???

My variable is BPM_Order_ID.

Then use Groovy string interpolation:

def BPM_Order_ID = "1251408"
def xpath = "//td[contains(.,${BPM_Order_ID})]//preceding-sibling::td/select"

It is not working, throwing an error Web element with id: ‘Object Repository/LR/Assign_SAP_Code/Select_User’ located by 'By.xpath: ’ not found)e[0;39m
2019-01-29 18:33:34.696 e[1;31mERRORe[0;39m e[36mc.k.katalon.core.main.TestCaseExecutor -e[0;39m e[31m❌ selectOptionByValue(findTestObject(“LR/Assign_SAP_Code/Select_User”), “avinash.patil”, false) FAILED.e[0;39m

I am using element Select_User having xpath as ‘//td[contains(.,‘1251408’)]//preceding-sibling::td/select’

Are you able to find the element in DevTools using that xpath?

Hello Mate

It is working when i am using static value 1251408 and xpath as //td[contains(.,‘1251408’)]//preceding-sibling::td/select

But when i use xpath with variable i.e //td[contains(.,${BPM_Order_ID})]//preceding-sibling::td/select it is throwing error; My dynamic xpath is in object Select_User

Below is the script; Here i am passing the value 1251408 in variable BPM_Order_ID through excel and then finding the element using variable.

System.out.println(BPM_Order_ID)

WebUI.scrollToElement(findTestObject(‘LR/Assign_SAP_Code/Select_User’), GlobalVariable.Timeout)

WebUI.selectOptionByValue(findTestObject(‘LR/Assign_SAP_Code/Select_User’), ‘avinash.patil’, false)

Below is the error:

2019-01-30 15:25:27.910 e[39mDEBUGe[0;39m e[36mtestcase.Assign_SAP_Code -e[0;39m e[39m12: out.println(BPM_Order_ID)e[0;39m
1251408
2019-01-30 15:25:27.917 e[39mDEBUGe[0;39m e[36mtestcase.Assign_SAP_Code -e[0;39m e[39m13: scrollToElement(findTestObject(“LR/Assign_SAP_Code/Select_User”), Timeout)e[0;39m
2019-01-30 15:26:04.733 e[34mINFO e[0;39m e[36mc.k.k.c.webui.common.WebUiCommonHelper -e[0;39m e[39mUnable to find the element located by ‘By.xpath: //td[contains(.,${BPM_Order_ID})]//preceding-sibling::td/select’. Please recheck the objects properties to make sure the desired element is located. e[0;39m
2019-01-30 15:26:05.230 e[1;31mERRORe[0;39m e[36mc.k.k.core.keyword.internal.KeywordMain -e[0;39m e[31m❌ Unable to scroll to object ‘Object Repository/LR/Assign_SAP_Code/Select_User’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/LR/Assign_SAP_Code/Select_User’ located by ‘By.xpath: //td[contains(.,${BPM_Order_ID})]//preceding-sibling::td/select’ not found)e[0;39m
2019-01-30 15:26:05.233 e[1;31mERRORe[0;39m e[36mc.k.katalon.core.main.TestCaseExecutor -e[0;39m e[31m❌ scrollToElement(findTestObject(“LR/Assign_SAP_Code/Select_User”), Timeout) FAILED.

Can you show me where is your test object defined? Object repository (take screenshot) or script (copy and paste)?

My variable BPM_Order_ID is defined in the test case and below is the script for that test case;
My test object Select_User is present in Object Repository;

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

WebUI.switchToFrame(findTestObject(‘LR/Raise_POS_Order/Navigation_Frame’), GlobalVariable.Timeout)

WebUI.waitForElementVisible(findTestObject(‘LR/Assign_SAP_Code/My_Tasks’), GlobalVariable.Timeout)

WebUI.click(findTestObject(‘LR/Assign_SAP_Code/My_Tasks’))

WebUI.switchToDefaultContent()

WebUI.switchToFrame(findTestObject(‘LR/Raise_POS_Order/Viewport_Frame’), GlobalVariable.Timeout)

WebUI.waitForElementVisible(findTestObject(‘LR/Assign_SAP_Code/Select_Group’), GlobalVariable.Timeout)

WebUI.selectOptionByValue(findTestObject(‘LR/Assign_SAP_Code/Select_Group’), ‘CashApps_L2’, false)

WebUI.delay(3)

WebUI.click(findTestObject(‘LR/Assign_SAP_Code/Cash_AppsL2_NI_New’))

WebUI.waitForElementNotVisible(findTestObject(‘LR/Assign_SAP_Code/Request_Being_Processed’), GlobalVariable.Timeout)

WebUI.waitForElementVisible(findTestObject(‘LR/Assign_SAP_Code/Section_Heading’), GlobalVariable.Timeout)

System.out.println(BPM_Order_ID)

WebUI.scrollToElement(findTestObject(‘LR/Assign_SAP_Code/Select_User’), GlobalVariable.Timeout)

WebUI.selectOptionByValue(findTestObject(‘LR/Assign_SAP_Code/Select_User’), ‘avinash.patil’, false)

not_run: WebUI.getText(findTestObject(‘LR/Assign_SAP_Code/BPM_Order_ID’), FailureHandling.STOP_ON_FAILURE)

If the object is in the Object Repository, you will need to parametrize it. See
https://docs.katalon.com/katalon-studio/docs/manage-test-object.html#parameterizing-test-object

Then report back if you have further questions.

2 Likes

Mate, U r great :slight_smile: It worked!
My only concern is that if i am using the test object twice and then for each instance i have to parameterized it or is there any different approach since this object will be used in other test cases as well… Here i have parameterized in step 13 & 14 both…

You should be able to use it multiple times.

It should look something like this first time you are selecting user for the field next to “1251408” in your first screenshog:

WebUI.scrollToElement(findTestObject(‘LR/Assign_SAP_Code/Select_User’, [('BPM_Order_ID'):"1251408"]), GlobalVariable.Timeout)

WebUI.selectOptionByValue(findTestObject(‘LR/Assign_SAP_Code/Select_User’), ‘avinash.patil’, false)

And something like this for the second user (let’s say you are again selecting ‘avinash.patil’, but this time for the field next to “1249260”):

WebUI.scrollToElement(findTestObject(‘LR/Assign_SAP_Code/Select_User’, [('BPM_Order_ID'):"1249260"]), GlobalVariable.Timeout)

WebUI.selectOptionByValue(findTestObject(‘LR/Assign_SAP_Code/Select_User’), ‘avinash.patil’, false)

Yes, but my question is everytime i have to parameterized the object for each step in test case.
Here i have done at step 13 also and 14 also…and the entire test case is for one BPM Order ID i,e 1251408. So parameterizing it at step 13 only should serve the purpose…right???

To be honest, I don’t know. :man_shrugging:

Why don’t you try and see what happens?

And let me know… :slight_smile:

OK mate, i will let you know;

Need further small help; how do i click on checkbox similary the way i have selected username from dropdown…What would be the xpath in this case???

The corresponding check box element is;

input id=“taskListForm:tasklist:890:rowcheckboxID” type=“checkbox” name=“taskListForm:tasklist:890:rowcheckboxID” onclick=“formTaskSelectedList(‘_TKI:a01b0168.980ab7c7.e9eb7af5.c25b9406’,this)”

Following the same logic,

'//td[contains(.,"1251408")]//preceding-sibling::td/input'

will select the checkbox.

You can read the xpath like

select the td that contains 1251408, //td[contains(.,"1251408")
and then find the preceding sibling of that element //td[contains(.,"1251408")]//preceding-sibling
that is also a td element //td[contains(.,"1251408")]//preceding-sibling::td
that also has an input element as the descendant //td[contains(.,"1251408")]//preceding-sibling::td/input

Hope that helps :smiley:

:slight_smile: It worked Mate, but i am struggling to find out how exactly this work???
For below scenario i am using //td[contains(.,‘1241408’)]//preceding-sibling::td/a …but it is not working now, what am i doing wrong here? I want to click on POS_OM_AssociateSAPCode

image

The corresponding element code is

POS_OM_AssociateSAPCode
1251408 NewILLAccenture BM LRTata Communications Limited Jan 29, 2019 10:47:42 AM Jan 29, 2019 10:47:39 AM 1111111 Non Integrated Category_NA

Are you using different HTML now? Because the one you posted yesterday doesn’t have an “a” element inside of that “td”.

Note that td/a means “a” that is a direct descendant of “td”. On the other hand, td//a would select “a” that is any-level descendant of “td”.

So, try //td[contains(.,‘1241408’)]//preceding-sibling::td//a (note the double “/” before “a”).