This is definitely doable. As you’ve stated, you essentially have 2 problems to solve:
1.) Find the index of the column with a given column name.
2.) Find the index of the row with some text.
Once you have these, you can target the cell you want with the row/column indices.
Lets try to tackle these individually:
1.) Find the index of the column with a given column name:
You need to do 2 things to solve this problem. First, get a list of the column names, and second, get the index of a column with a given name. I would probably put these in their own Custom Keywords, as there are probably other times where you’d want to use them:
@Keyword
public List<String> getColumnNames() {
WebDriver driver = DriverFactory.getWebDriver();
List<WebElement> elements = driver.findElements(By.xpath("//div[contains(@class, 'slick-headerrow')]//span[contains(@class, 'slick-column-name')]"));
List<String> columnNames = new ArrayList<String>();
for (WebElement element : elements) {
columnNames.add(element.getAttribute("textContent"));
}
return columnNames;
}
@Keyword
public int getColumnIndex(String columnName) {
return getColumnNames.indexOf(columnName) + 1;
}
2.) Find the index of the row with some text.
For this one, the standard approach is to loop through all rows visible in the table, checking if the row contains some text, and finding its index. From this you have the row index you want, then you can check the value of the cell in this row and the column index you found in part 1 above. Again, you could have separate keywords here, one to find the index of a row with some text, and another to click a cell based on a row/column index:
@Keyword
public int getRowIndex(String rowTextIdentifier) {
WebDriver driver = DriverFactory.getWebDriver();
List<WebElement> elements = driver.findElements(By.xpath("//div[contains(@class, 'slick-row')]"));
for(int i = 1; i <= elements.size(); i++) {
if(elements.getAttribute("textContent").contains(rowTextIdentifier)) {
return i;
}
}
// return -1 if text isn't found:
return -1;
}
@Keyword
public void clickCell(int rowIndex, int columnIndex) {
WebDriver driver = DriverFactory.getWebDriver();
WebElement cell = driver.findElement(By.xpath("((//div[contains(@class, 'slick-row')])[" + rowIndex + "]//div[contains(@class, 'slick-cell')])[" + columnIndex + "]"));
cell.click();
}
Putting it all together:
In your script, you can now do this:
int rowIndex = getRowIndex("PL-Planning documentation");
int columnIndex = getColumnIndex("Duration");
clickCell(rowIndex, columnIndex);
I’m not expecting everything above to work on the first try, we will likely need to make adjustments, but the general approach should hopefully be a little clearer now. Let me know how it goes 