Created
July 18, 2012 19:59
-
-
Save raygt/3138501 to your computer and use it in GitHub Desktop.
Selenium 2 Webdriver Iterating Table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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