Get value of a second table cell with unique text in first cell of the row with Watir
I want to get the value in the second cell of a row of a table, where a unique value is in the first cell. The value of the second cell keeps on changing.
<table class="mm-table" cellspacing="0" cellpadding="3" border="0">
<tr class="trhover" onclick="location.href='stock.php?id=RI&p=wtch';">
<td align="left" rowspan="1" class="tdwidth35per paddingleft4px">
<a shape="rect" href="stock.php?id=RI&p=wtch"> Reliance </a></td>
<td align="right" rowspan="1" class="tdwidth35per"> **951.35** </td>
<td align="right" rowspan="1" class="tdwidth30per"><font color="#008000">0.60</font></td>
</tr>
Above is the table. There are other tables with the same name. I want to access 开发者_如何学Gothe second cell which contains the text "951.35"
See if this causes the cell you want to flash
browser.cell(:text, " **951.35** ").parent.cell(:index, 2).flash
or if you only know a part of the text that will be in that cell, try this
browser.cell(:text, /951.35/).parent.cell(:index, 2).flash
If for some reason using .parent won't work you can also just try matchng a portion of the text on the entire row
browser.row(:text, " **951.35** ").parent.cell(:index, 2).flash
Note that in the case of firewater which uses zero based indexing I believe you would need to change the index value to 1
Also note that this would only work if there is only a single cell on the page with the specified text, especially in the case of the last two examples that are using a regular expression to match the text you want. In that case if there is another cell with those numbers, even if it's part of another value such as 12951.356 Watir would not know which one you wanted to match, and would give you an error.
If any of those work to flash the cell you want, then just replace .flash with .text to get the text value in the cell.
Try something like this:
browser.table(:class => "mm-table").cell(:class => "tdwidth35per")
精彩评论