Surendra Suthar
Surendra Suthar

Reputation: 370

How to get elements from table in Selenium Web Driver JAVA

I am trying to Identify Elements of the page. I have table and there is lot of rows with multiple Inputs. How to access Input from table rows?

This is my java class

public class Setting extends Page {

    /**Identify Elements of the page*/

    @FindBy(id="table-settings")

    private WebElement Table;

    //Constructor
    public Setting()
    {
        /**Initialize PageFactory WebElements*/
        PageFactory.initElements(driver, this);
    }   

}

Upvotes: 0

Views: 2889

Answers (2)

Test admin
Test admin

Reputation: 731

Try this: You can try with finding the specific input element with xpath instead of id. If id available for specific element, you can use id itself. Also in your constructor, try to change your code from

public Setting()
{
    /**Initialize PageFactory WebElements*/
    PageFactory.initElements(driver, this);
}   

to

public Setting(WebDriver driver)
{
  this.driver = driver;
    /**Initialize PageFactory WebElements*/
    PageFactory.initElements(driver, this);
}   

and in your class try adding

WebDriver driver;

before your @FindBy statement.

Upvotes: 0

Gaurav Lad
Gaurav Lad

Reputation: 1808

Refer below code, make changes accordingly, Let me know if you find any hurdle in it

@Test
public void test() {
    WebElement Webtable=driver.findElement(By.id("TableID")); // Replace TableID with Actual Table ID or Xpath

    List<WebElement> TotalRowCount=Webtable.findElements(By.xpath("//*[@id='TableID']/tbody/tr"));

    System.out.println("No. of Rows in the WebTable: "+TotalRowCount.size());
    // Now we will Iterate the Table and print the Values   

    int RowIndex=1;

    for(WebElement rowElement:TotalRowCount)
    {
        List<WebElement> TotalColumnCount=rowElement.findElements(By.xpath("td"));
        int ColumnIndex=1;

        for(WebElement colElement:TotalColumnCount)
        {
            System.out.println("Row "+RowIndex+" Column "+ColumnIndex+" Data "+colElement.getText());
            ColumnIndex=ColumnIndex+1;
        }
        RowIndex=RowIndex+1;
    }
}

Upvotes: 1

Related Questions