How to find the xpath of a tabel cell in relation of table column header and row header

This is the image that I want to find out the xpath of the check box in relation of Column header and row header
xpath

Hi,

general approach would be:

  1. Get an index of column you want (in your case “Day”)
  2. Use this index in XPath for td attribute for Evening row.

Thank you for the response
Yes we can find out using row and column index
But
In my case this is the dynamic xpath using parameterization
Here the requirement is I want to find the check box using parameterized values Column header
and Row header(Ex: Evening and Day)
Because I want to dynamically change the check box by parameterized values

:heavy_check_mark:If you give the solution it will be a big helpful for me

I see. I’d do following:

  1. Get two maps containing pairs column(row)Name:index
  2. Create custom keyword void checkCombination(columnName, rowName)
  3. Within this keyword, get indexes of both parameters from previously created maps
  4. Use those indexes in dynamic XPath to check correct checkbox
1 Like

Thanks
if you can plz just give brief

Hi @Marek_Melocik
I need just brief about point 1

  1. Get two maps containing pairs column(row)Name:index
    please give some descriptions

Example:

WebUI.openBrowser('https://www.w3schools.com/html/html_tables.asp')

String tableHeaderPath = "//table[@id='customers']//th"
List<WebElement> headers = DriverFactory.getWebDriver().findElements(By.xpath(tableHeaderPath))
Map indexOfHeaders = new HashMap()

int i = 1
for(WebElement header in headers) {
	indexOfHeaders.put(header.getText(), i)
	i++
}

println indexOfHeaders

Result:

[Company:1, Country:3, Contact:2]

Do the same for row labels.

And then, to get an index of header/row in the main custom keyword, just call
int companyIndex = indexOfHeaders.get("Company")

1 Like

1

Results
getColumnHeader

[Medical ICU - Coverage:2, :12, Evening:4, Night:5, Medical ICU
Shifts:11, Day:3]

getRowLabel

[Evening:2, Night:3, Day:1]

Hi @Marek_Melocik
I am getting results like this using this how can I change the check box dynamically
and above things is it correct…?is there any suggestions plz …?

Row indexes looks good, but you should fix XPath for columns. Can you share source code of that table?

Sure i ll send

Medical ICU
Shifts
Medical ICU - Coverage
Day Evening Night
Medical ICU
Shifts
Medical ICU - Coverage
Day
Evening
Night
Day
Evening
Night
Medical ICU
Shifts
Day
Evening
Night

What about this xpath:

//div[@class="dataTables_scrollHead"]//tr[@role="row"][2]

Does it return correct column names?

getColumnHeader()

[:6, Day Evening Night:2]

this was the results

2019-08-07 17:43:48.475 DEBUG testcase.test- - 1: com.helper.common.CommonUnit.getColumnHeader()
[Evening:2, Night:3, Day:1]
2019-08-07 17:43:50.751 INFO k.k.c.m.CustomKeywordDelegatingMetaClass - com.helper.common.CommonUnit.getColumnHeader is PASSED
2019-08-07 17:43:50.751 DEBUG testcase.test- - 2: com.helper.common.CommonUnit.getRowLabel()
[Evening:2, Night:3, Day:1]
2019-08-07 17:43:50.857 INFO k.k.c.m.CustomKeywordDelegatingMetaClass - com.helper.common.CommonUnit.getRowLabel is PASSED

Now is it ok…?what can I do next…?

I used this Xpath"//div[@id=‘shiftAndCoverageTable_wrapper’]//div[@class=‘dataTables_scrollHeadInner’]//tr[@role=‘row’][2]//th"

I am struggle with the next step…

Now, find an XPath for the checkbox you want to check. It should contain tr[rowIndex] and td[columnIndex]. You should be able to add indexes dynamically from your Map instances.

But getColumnHeader() out put like this
[Evening:2, Night:3, Day:1]

and
getRowLabel()
[Evening:2, Night:3, Day:1]

This the Xpath of that check box
//div[@id=‘shiftAndCoverage’]//tr[1]//td[2]//input/…

We got already for Column header Day index is 1 and Row label Day index is also 1 but in the xpath in td tag we have to put 2, how can we manage this

void checkCombination(columnName, rowName) {
	// first td element is row label, so first checkbox index is 2
	int columnIndex = getColumnHeader(columnName) + 1
	int rowIndex = getRowLabel(rowName)
	
	String checkboxXpath = "//div[@id=‘shiftAndCoverage’]//tr[" + rowIndex + "]//td[" + columnIndex + "]//input/…"
	
	TestObject dynamicCheckbox = new TestObject()
	dynamicCheckbox.addProperty("xpath", ConditionType.EQUALS, checkboxXpath)
	WebUI.click(dynamicCheckbox)
}
1 Like

Thank you So much @Marek_Melocik
Now I have got the the point I ll try and let you know the out put…:+1:

Start recording video for test case ‘Test Cases/Unit/tc_sampleCheckboxClick’.
2019-08-08 12:07:58.321 DEBUG testcase.tc_sampleCheckboxClick - 1: com.helper.common.CommonUnit.getColumnHeader()
[Evening:2, Night:3, Day:1]
2019-08-08 12:08:00.185 INFO k.k.c.m.CustomKeywordDelegatingMetaClass - com.helper.common.CommonUnit.getColumnHeader is PASSED
2019-08-08 12:08:00.186 DEBUG testcase.tc_sampleCheckboxClick - 2: com.helper.common.CommonUnit.getRowLabel()
[Evening:2, Night:3, Day:1]
2019-08-08 12:08:00.262 INFO k.k.c.m.CustomKeywordDelegatingMetaClass - com.helper.common.CommonUnit.getRowLabel is PASSED
2019-08-08 12:08:00.262 DEBUG testcase.tc_sampleCheckboxClick - 3: com.helper.common.CommonUnit.checkCombination(data_columnName, data_rowName)
2019-08-08 12:08:00.295 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - :x: groovy.lang.MissingMethodException: No signature of method: com.helper.common.CommonUnit.getColumnHeader() is applicable for argument types: (java.lang.String) values: [Night]
Possible solutions: getColumnHeader()
2019-08-08 12:08:00.313 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/Unit/tc_sampleCheckboxClick FAILED.
Reason:
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: com.helper.common.CommonUnit.getColumnHeader() is applicable for argument types: (java.lang.String) values: [Night]
Possible solutions: getColumnHeader()
at com.helper.common.CommonUnit.invokeMethod(CommonUnit.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
at tc_sampleCheckboxClick.run(tc_sampleCheckboxClick:21)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestCaseMainPhase(TestSuiteExecutor.java:133)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:116)
at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:83)
at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:157)
at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
at TempTestSuite1565246255106.run(TempTestSuite1565246255106.groovy:35)
Caused by: groovy.lang.MissingMethodException: No signature of method: com.helper.common.CommonUnit.getColumnHeader() is applicable for argument types: (java.lang.String) values: [Night]
Possible solutions: getColumnHeader()
at com.helper.common.CommonUnit.checkCombination(CommonUnit.groovy:185)
… 16 more

I am facing this issue now

@Keyword
public void getColumnHeader(){

	Thread.sleep(500);
	WebUI.click(findTestObject('Page_Unit/Tab_Configuration/test_btn_configTabLink'))
	Thread.sleep(500);
	WebUI.click(findTestObject('Page_Unit/Tab_Configuration/test_btn_shiftAndCoverageTab'))
	Thread.sleep(500);

	String tableHeaderPath = "//div[@id='shiftAndCoverageTable_wrapper']//div[@class='dataTables_scrollHeadInner']//tr[@role='row'][2]//th"
	List<WebElement> headers = DriverFactory.getWebDriver().findElements(By.xpath(tableHeaderPath))
	Map indexOfHeaders = new HashMap()

	int i = 1
	for(WebElement header in headers) {
		indexOfHeaders.put(header.getText(), i)
		i++
	}

	println indexOfHeaders

}

@Keyword
public void getRowLabel(){

	String tableRowHeadLabelPath = "//div[@id='shiftAndCoverage']//div[@class='DTFC_LeftWrapper']//td"
	List<WebElement> headers = DriverFactory.getWebDriver().findElements(By.xpath(tableRowHeadLabelPath))
	Map indexOfHeadLabels = new HashMap()

	int i = 1
	for(WebElement header in headers) {
		indexOfHeadLabels.put(header.getText(), i)
		i++
	}

	println indexOfHeadLabels

}

@Keyword
void checkCombination(data_columnName, data_rowName) {
	// first td element is row label, so first checkbox index is 2
	int columnIndex = getColumnHeader(data_columnName) + 1
	int rowIndex = getRowLabel(data_rowName)
	
	String checkboxXpath = "//div[@id=‘shiftAndCoverage’]//tr[" + rowIndex + "]//td[" + columnIndex + "]//input/…"
	
	TestObject dynamicCheckbox = new TestObject()
	dynamicCheckbox.addProperty("xpath", ConditionType.EQUALS, checkboxXpath)
	WebUI.click(dynamicCheckbox)
}

This is my full script is there any issu…?