我正在使用Watin进行IE浏览器的自动化测试。我是Watin的新手。我被阻止了获取表的列标题文本的任务。该表的HTML代码如下所示:
<table id="gvVoiceCallReport" style="width: 100%; border-collapse: collapse; height: 100%;" border="1" rules="all" cellSpacing="0" cellPadding="0">
<tbody>
<tr style="background-color: slategray; height: 20px; color: white;">
<th scope="col">
<a style="color: white;" href="javascript:__doPostBack('gvVoiceCallReport','Sort$Caller GCI')">
Text - Caller GCIstyle
<th scope="col">
<a style="color: white;" href="javascript:__doPostBack('gvVoiceCallReport','Sort$Callee GCI')">
Text - Callee GCI
<th scope="col">
<a style="color: white;" href="javascript:__doPostBack('gvVoiceCallReport','Sort$Originator-Default Bearer Apn')">
Text - Originator-Default Bearer Apn
<tr style="cursor: hand;" onmouseover="this.style.cursor='hand';">
<td align="left" onclick="window.open('TestResult.aspx?testSuite=Job.139.1_1504100010110027884023126', 'TestResult', 'height=600,width=900,resizable=yes,scrollbars=yes');">
Text - 310;410;FFFE;9210;B9F
<td align="left" onclick="window.open('TestResult.aspx?testSuite=Job.139.1_1504100010110027884023126', 'TestResult', 'height=600,width=900,resizable=yes,scrollbars=yes');">
Text - 310;410;FFFE;9210;B9F
.....
......
</table>
该列的标题文字为来电者GCI 。 我能够通过使用类似
的内容获取该列中值的文本string columnValueText = mytable.OwnTableRows[1].TableCells[1].Text;
当我试图通过使OwnTableRows[0]
(索引为零)来获取列标题文本时,它给了我异常:数组超出范围。
任何人,请帮我获取表格列标题文字。
答案 0 :(得分:3)
var gridTableRows = page.Document.Frame(Find.ById("ctl00_MainContentPlaceHolder_ifrmReport")).Table("gvVoiceCallReport").TableRows[0];
StringCollection abc = GetAllColumnDataFromRow(gridTableRows, true);
private StringCollection GetAllColumnDataFromRow(TableRow tableRow, bool isTableHeaderRow)
{
StringCollection RowValues = new StringCollection();
if (isTableHeaderRow)
{
foreach (Element e in tableRow.Elements)
{
if (e.TagName == "TH")
{
RowValues.Add(e.Text);
}
}
}
return RowValues;
}
答案 1 :(得分:1)
我拿了这段HTML。将其粘贴到index.html。
我使用过的代码:
FireFox ie = new FireFox(@"D:\index.html");
Table tb = ie.Table(Find.ById("gvVoiceCallReport"));
if (tb != null)
{
Debug.WriteLine("Found table!");
var value = tb.Text;
TableRow tr = tb.TableRows[0];
Debug.WriteLine(tb.Text);
foreach (var elem in tb.Elements)
{
Debug.WriteLine("Name : {0}, Type : {1}",elem.Name,elem.GetType());
if (elem.GetType().ToString().Equals("WatiN.Core.Link"))
{
Debug.WriteLine(elem.Text);
//elem.Text is current hader;
}
}
}
导致调试输出:
Text - Caller GCIText - Caller GCIstyleText - Originator-Default Bearer Apn
Name : , Type : WatiN.Core.TableBody
Name : , Type : WatiN.Core.TableRow
Name : , Type : WatiN.Core.ElementContainer`1[WatiN.Core.Element]
Name : , Type : WatiN.Core.Link
Text - Caller GCI
Name : , Type : WatiN.Core.ElementContainer`1[WatiN.Core.Element]
Name : , Type : WatiN.Core.Link
Text - Caller GCIstyle
Name : , Type : WatiN.Core.ElementContainer`1[WatiN.Core.Element]
Name : , Type : WatiN.Core.Link
Text - Originator-Default Bearer Apn
我希望这很有帮助。