Sunday, 16 March 2014

How to read data from web table in selenium webdriver in C#.Net?


Reading the data from the table is very easy in selenium webdriver.

We can identify the table using name, Id or xpath and then we can access the rows one by one using findElements method.

For example – below statement will find all row elements from the given table. Please note that t stands for the table object you have found using findElement method.

ReadOnlyCollection <IWebElement> ec
= t.FindElements(By.TagName("tr"));

Another example – Below example illustrates how we can find the column number  for the given column name in a table.

int getColumnNumber(IWebElement r, String columnName )
{

ReadOnlyCollection <IWebElement> cells = r.FindElements(By.TagName("th"));
int c = 0;

for (int k=0;k<cells.Count;k++)
 c=c+1;
Console.WriteLine(c + " --> "+ cells[k].Text );
 if (columnName.Equals(cells[k].Text))
break;
}

return c;
}


Another example – Below example illustrates how we can check if the value in given cell matches the desired value.

Below function takes 3 parameters. First parameter is row element. Second parameter is the column number and third parameter is expected value.


boolean verifyValue (IWebElement r, int a, String expValue)
{
ReadOnlyCollection <IWebElement>  mcells = r.FindElements(By.TagName("td"));
int c = 0;

for (int k=0;k<mcells.Count;k++)
c=c+1;
if (c==a)
{

//we can get the value inside cell using getText() method.

if (expValue.Equals(mcells[k].Text))
return true;

}

 }
 return false;


}

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

About Me

My photo

Self-motivated software professional