如何在 Watin 中获取 table 列 header 文本?
How to get table column header text in Watin?
我正在使用 Watin 对 IE 浏览器进行自动化测试。我是 Watin 的新手。我在为 table.HTML 代码获取列 header 文本的任务中受阻,table 如下所示:
<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>
Header 该列的文本是 Caller GCI。
我可以使用类似的方法获取该列中值的文本
string columnValueText = mytable.OwnTableRows[1].TableCells[1].Text;
当我试图通过使 OwnTableRows[0]
(索引为零)来获取列 header 文本时,它给了我异常:数组越界。
任何人,请帮助我获取 table 列 header 文本。
这张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
希望对您有所帮助。
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;
}
我正在使用 Watin 对 IE 浏览器进行自动化测试。我是 Watin 的新手。我在为 table.HTML 代码获取列 header 文本的任务中受阻,table 如下所示:
<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>
Header 该列的文本是 Caller GCI。 我可以使用类似的方法获取该列中值的文本
string columnValueText = mytable.OwnTableRows[1].TableCells[1].Text;
当我试图通过使 OwnTableRows[0]
(索引为零)来获取列 header 文本时,它给了我异常:数组越界。
任何人,请帮助我获取 table 列 header 文本。
这张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
希望对您有所帮助。
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;
}