Sunday, March 5, 2017

Selenium WebDriver – Exploring the Table element (C#)

HTML tables are used to contain a data that should be displayed in a specific logical order to the end user, the following paragraphs will describe both the basic and advanced commands that you need to when working with this element. know.
תמונה קשורה

HTML table
An HTML table is a collection of rows <tr>, headers<th> and columns <td>, each <tr> element is the root of on ore more columns <td>.

The code examples are based on the following table: 


How to Extract all data from a table using rows(tr)

The following code will go through each row and print the associated values (Each column value is printed per iteration). 

IWebElement TempElement;
IWebDriver Firefox = new FirefoxDriver();
Firefox.Navigate().GoToUrl("http://www.machtested.com/");
Firefox.FindElement(By.XPath("//a[contains(@href,'selenium-course')]")).Click();
TempElement = Firefox.FindElement(By.ClassName("MsoNormalTable"));
IList<IWebElement> ListOfElements = TempElement.FindElements(By.TagName("tr"));

foreach (var item in ListOfElements)
{
sw.WriteLine(item.Text.ToString());
}

Result:













How to Extract all data from a table using columns(td)

Same as the previous example, but now we will change the tag from 'tr' into 'td'
The line of code that was changed: 

IList<IWebElement> ListOfElements = TempElement.FindElements(By.TagName("td"));


How to Identify data located on specific cell

TempElement = Firefox.FindElement(By.XPath("XPath/CSS of a specific cell"));

How to Get the number of Rows in a table

The following code will help us to retrieve the number of columns and rows of the table.

Code:

TempElement = Firefox.FindElement(By.ClassName("MsoNormalTable"));
IList<IWebElement> Table_Rows = TempElement.FindElements(By.TagName("tr"));
sw.WriteLine(Table_Rows.Count;);

Result:
Rows: 20 

How to Identify each cell based on a unique Row and column id

Code:
[TestMethod]
public void PrintAllRows()
{
StreamWriter sw = new StreamWriter(@"C:\Users\Administrator\Google Drive\1\Debugg.txt", true);
IWebElement TempElement;
IWebDriver Firefox = new FirefoxDriver();
Firefox.Navigate().GoToUrl("http://www.machtested.com/");
Firefox.FindElement(By.XPath("//a[contains(@href,'selenium-course')]")).Click();
int row_tr = 20;
int Column_td = 3;
String CellValue;

for (int i = 1; i <= row_tr; i++)
{
for (int b = 1; b < Column_td; b++)
{
CellValue = @"Row ID ="+i+"|Column ID="+b+"|Cell Content="+ Firefox.FindElement(By.XPath(@"/html/body/div[3……div[2]/table/tbody/tr["+i+"]/td["+b+"]/div")).Text;
sw.WriteLine(CellValue + Environment.NewLine);
}
}
sw.Close();
}

Result:
Row ID = 1 | Column ID = 1 | Cell Content = Lesson
Row ID = 1 | Column ID = 2 | Cell Content = Article Name
Row ID = 2 | Column ID = 1 | Cell Content = Lesson #1
Row ID = 2 | Column ID = 2 | Cell Content = Why should you learn selenium?

How to filter the returned values, Examples:

Filter using And / Or

if (CellValue == "Expected string" || CellValue == "Expected string")
{//Do something}

if (CellValue == "Expected string" && CellValue == "Expected string")
{//Do something}

Filter based on specific Row/Column or

if (i == RowID && b == ColumnID)
{//Do something}

if (b == ColumnID)
{//Do something}

if (i == RowID)
{//Do something}


No comments:

Post a Comment

My Presentations