Just wondering if there is a better way to get values from a table in selenium 2. I am currently using 2 for loops I loop over each TR and within each TR I loop over all TD. so for example if I have a table row with 10 columns I loop 10 times and pull out the text value. That seems clunky to me.
My table Rows looks like so
<tr id="cTestData" class="odd">
<td class="date_activated">08/31/2011</td>
<td class="date_redeemed"> Not redeemed * </td>
<td class="expiration_date">09/01/2011</td>
<td class="product"> State of Maine </td>
<td class="value">$1.00</td>
<td class="store"> – – – </td>
<td class="offer_details">
</tr>
I think I should be able to say for each table Row get me the TD element with class = date_activated and have it return the date. I tried a few things but nothing seemed to work based on TD class name = foo
If it helps my actual code is
for(WebElement trElement : tr_collection)
{
List<WebElement> td_collection=trElement.findElements(By.xpath("td"));
System.out.println("NUMBER OF COLUMNS="+td_collection.size());
col_num=1;
HashMap actInfo = new HashMap(); // new hashmap for each line inthe result set
if(!td_collection.isEmpty() && td_collection.size() != 1 ){
for(WebElement tdElement : td_collection)
{
开发者_运维知识库 System.out.println("Node Name=== " + tdElement.getAttribute("class"));
System.out.println("Node Value=== " + tdElement.getText());
actInfo.put(tdElement.getAttribute("class"), tdElement.getText());
col_num++;
}
masterMap.add(actInfo);
} // end if
row_num++;
}
Try this:
driver.findElements(By.xpath("//tr[@class='foo']/td[@class='date_activated']"))
That will return all the TD elements with the class date_activated
with a parent row with class foo
. You can then loop through the elements and use getText
to get the dates. This works from the root of the page.
If you would like to do it from each TR element, try:
trElement.findElement(By.xpath("./td[@class='date_activated']")).getText()
I found it easier to work with tables as a table. You still have to use the XPath, but it's limited to the table.
IWebElement table = driver.FindElement(By.Id("TableId")); //Get Table
List<IWebElement> Rows = new List<IWebElement>(table.FindElements(By.XPath(".//tbody/tr")));
List<List<IWebElement>> table_element = new List<List<IWebElement>>();
for (int k = 0; k < Rows.Count; k++)
{
table_element.Add(new List<IWebElement>(Rows[k].FindElements(By.XPath("./td")))); //Get all Elements from Rows
}
for (int k = 0; k < table_element[0].Count; k++)
{
if (table_element[0][k].Text == "08/31/2011")
{
table_element[0][k].Click();
}
}
If you prefer to use a css selector, try:
List<WebElement> myTds = driver.findElements(By.cssSelector("#tableId .date_activated"));
Note the space in "#tableId .date_activated"
.
This will select all the elements with class date_activated
within a table with an id tableId
. You will still need to loop over this list to get the text of each of your cells.
A little simpler selector might be sufficient:
driver.findElements(By.cssSelector(".date_activated"))
This will find all the elements with class date_activated
on your page.
精彩评论