Ahmad Odeh
Ahmad Odeh

Reputation: 47

Selenium Webdriver - How to select record from grid view

How can I select specific record from grid view using selenium webdriver (JAVA)

Suppose I want to select the highlighted record in this snapshot. How I can do that:

Upvotes: 0

Views: 9189

Answers (2)

Zeeshan S.
Zeeshan S.

Reputation: 2091

You should systematically break down the grid into sections, loop through each row's cell to find a match using a unique key and then select the row. In this case, the unique key is the Employee Number column.

In Java:

public void selectRow(String expEmpNo) {
    // Get the grid
    WebElement grid = driver.findElement(By.id("gpUsers"));

    // Get all the rows
    By locAllRows = By.xpath(".//*[contains(@class,'x-grid3-body')]//*[contains(@class,'x-grid3-row')]");
    List<WebElement> allRows = grid.findElements(locAllRows);

    // Loop through each row and compare actual emp. no. with expected emp. no.
    for(WebElement row : allRows) {
        // Emp No. is 4th column
        By locEmpNo = By.xpath(".//*[@class='x-grid3-cell-inner x-grid3-col-4']");
        // Get the Emp. No.
        String actEmpNo = row.findElement(locEmpNo).getText();
        // Compare actual vs expected
        if(actEmpNo.equals(expEmpNo)) {
            row.click(); // Select row
            System.out.println("Selected row " + (allRows.indexOf(row) + 1) + " having Emp. No. " + expEmpNo)
            break;  // exit the for loop
        }
    }
}

Upvotes: 3

shavikH
shavikH

Reputation: 5

I believe that you need something like this

WebElement element = driver.findElement(By.xpath("use the xpath here"));
Select oSelect = new Select(element);
oSelect.selectByVisibleText("enter the visible text you want to select");

Upvotes: -2

Related Questions