Skip to content

Instantly share code, notes, and snippets.

@raygt
Created July 18, 2012 19:59
Show Gist options
  • Save raygt/3138501 to your computer and use it in GitHub Desktop.
Save raygt/3138501 to your computer and use it in GitHub Desktop.
Selenium 2 Webdriver Iterating Table
I found example 2 on the web but it was not exactly what i needed. so.. I put together example 1.
C# Example 1
public void VerifyTable(string header, string expected)
{
IWebElement table = _driverWithJs.FindElement(By.XPath("//div[@id='main']/table"));
ReadOnlyCollection<IWebElement> allRows = table.FindElements(By.TagName("tr"));
for (int z = 0; z < allRows.Count; z++)
{
ReadOnlyCollection<IWebElement> cells = allRows[z].FindElements(By.TagName("td"));
for (int y = 0; y < cells.Count; y++)
{
var value = allRows[z].FindElements(By.TagName("td"))[y].Text;
if (value.Equals(header))
{
Assert.AreEqual(expected, allRows[z].FindElements(By.TagName("td"))[y + 1].Text);
}
}
}
}
C# Example 2
IWebElement table = _driverWithJs.FindElement(By.XPath("//div[@id='main']/table"));
ReadOnlyCollection<IWebElement> allRows = table.FindElements(By.TagName("tr"));
foreach (IWebElement row in allRows)
{
ReadOnlyCollection<IWebElement> cells = row.FindElements(By.TagName("td"));
foreach (IWebElement cell in cells)
{
Console.WriteLine("\t" + cell.Text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment