Search code examples
javaselenium-webdriverxpathselenium-griddynamic-tables

Selenium : Dynamic Table


driver.get("https://www.leafground.com/dynamicgrid.xhtml");
        
//count column
List<WebElement> column = driver.findElements(By.tagName("th"));
System.out.println(column.size());
        
//row 
List<WebElement> row = driver.findElements(By.tagName("tr"));
System.out.println(row.size()/2);
        
//return value of a customer
String text = driver.findElement(By.xpath("//td[normalize-space()='Costa Jennifer']//td[3]")).getText();
System.out.println(text);

What I'm trying to do is to get the activity value for Costa Jennifer value. But I'm getting:

Unable to locate the element.


Solution

  • You need to improve your locators.
    This will give you the table rows:

    List<WebElement> rows = driver.findElements(By.xpath("//tbody//tr[@role='row']"));
    System.out.println(rows.size());
    

    To get activity value of some user you can locate the row by user name and then locate proper td cell value. As following:

    String activity = driver.findElement(By.xpath("//tr[contains(.,'Munro Leon')]//td[4]")).getText();
    System.out.println(activity);