How to click on elements which are located in cell of the table Selenium

Yamis :

I wrote method that gets text value from cell ​​by row number and column name.

public String getValueFromCell(int rowNumber, String columnName){
        List<Map<String, WebElement>> rowsWithColumnsByHeadings = getRowsWithColumnsByHeadings();
        return rowsWithColumnsByHeadings.get(rowNumber - 1).get(columnName).getText();
    }

But there are icons in cells(as you can see in the screenshot, there can be one,two, or four icons) Icons with Xpath

And now i want to click on all of them. Or click on specific icon

And i must admit that this table is located on many pages with different count of icons in cell, and i want to use this method for all of them.

How can i modify this method? Or what should i do?

Dipak Bachhav :

Please check below solution:

If you want to click on specific icon element then you can use rowNumber index to click on it, Just check otherwise use moveToElement using Action class before while clicking onto the element:

public String getValueFromCell(int rowNumber, String columnName){
        List<Map<String, WebElement>> rowsWithColumnsByHeadings = getRowsWithColumnsByHeadings();
        List<WebElement> elements = driver.findElements(By.className("mat-icon.mat-icon.notranslate.material-icon.mat-icon-n0-color"))
        elements.get(rowNumber).click()
        return rowsWithColumnsByHeadings.get(rowNumber - 1).get(columnName).getText();
    }

or if you want to click all of them one by one :

    List<WebElement> elements = driver.findElements(By.className("mat-icon.mat-icon.notranslate.material-icon.mat-icon-n0-color"));
    elements.forEach(e -> {
        e.click();

    });

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=362040&siteId=1