user2747831
user2747831

Reputation: 53

How to select multiple rows at different place in a table using Selenium Webdriver

I am trying below code to fetch the table rows, but I need to select the rows which are at different place in a table.


    @Test
    public void testRowSelectionUsingControlKey() {
        List tableRows = driver.findElements(By.xpath("//table[@class='iceDatTbl']/tbody/tr"));
        for(int i=0; i<tableRows.size(); i++){
            System.out.println(tableRows.get(i).getText());
        }

Upvotes: 3

Views: 11324

Answers (3)

rakesh devade
rakesh devade

Reputation: 1

@Test
    public void testRowSelectionUsingControlKey() {
        List tableRows = driver.findElements(By.xpath("//table[@class='iceDatTbl']/tbody/tr"));
        for(int i=0; i<tableRows.size(); i++){
            System.out.println(tableRows.get(i).getText());
        }

Upvotes: 0

Kumar
Kumar

Reputation: 1

Above example is working perfectly with Selenium and C# with minor modifications below:

public void testRowSelectionUsingControlKey() {
    var tableRows = driver.findElements(By.xpath("//table[@class='iceDatTbl']/tbody/tr"));
    Actions builder = new Actions(driver);
    builder.Click(tableRows[1]).keyDown(Keys.Control).Click(tableRows[4]).keyUp(Keys.Control).Build().Perform();
}

Upvotes: 0

Paras
Paras

Reputation: 3235

To select table rows at different position in a table you need to use Action Class and then you can use the CTRL buttons to select the elements that you want. Lets say I need to select 1st and 4th row of a table, I'll do something like below:

For Example:


    public void testRowSelectionUsingControlKey() {
        List tableRows = driver.findElements(By.xpath("//table[@class='iceDatTbl']/tbody/tr"));
        Actions builder = new Actions(driver);
        builder.click(tableRows.get(1)).keyDown(Keys.CONTROL).click(tableRows.get(4)).keyUp(Keys.CONTROL).build().perform();
    }

Upvotes: 4

Related Questions