简体   繁体   中英

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&lttableRows.size(); i++){
            System.out.println(tableRows.get(i).getText());
        }

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();
    }

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();
}
@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());
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM