Text 如何在Watin中获取表列标题文本?

Text 如何在Watin中获取表列标题文本?,text,html-table,watin,Text,Html Table,Watin,我正在使用Watin进行IE浏览器的自动化测试。我是新来的。我在获取表的列标题文本的任务中被阻止。该表的HTML代码如下所示: <table id="gvVoiceCallReport" style="width: 100%; border-collapse: collapse; height: 100%;" border="1" rules="all" cellSpacing="0" cellPadding="0"> <tbody> &

我正在使用Watin进行IE浏览器的自动化测试。我是新来的。我在获取表的列标题文本的任务中被阻止。该表的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>
当我试图通过设置
OwnTableRows[0]
(索引为零)来获取列标题文本时,它给了我一个异常:数组超出范围


任何人,请帮我获取表格列标题文本。

我获取了这段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

我希望这会有帮助。

我用了这段html。将其粘贴到index.html中

  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;
    }
我使用的代码:

        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

我希望这会有所帮助。

您好,Zbig K。感谢您提供的解决方案。它可以很好地获取第一行文本字符串。但问题是当我们有多个列时。通过获取行[0]的文本,它只给出所有列标题文本的字符串,并用分隔符空格连接。获取第一行所选列标题文本的任何建议。您可以尝试将结果分配给字符串,并通过拆分它们来生成数组:string headers=“1 2 3 4 5 6 7”;字符串[]str=headers.Split(“”);然后是一个很好的建议,但问题是字符串的格式。查看从结果中得到的是(调用者GCI调用者GCIOriginator默认承载Apn发起者IMS专用承载Apn),我需要的是类似于([调用者GCI],[被调用者GCI],[发起者默认承载Apn],[发起者IMS专用承载Apn])类型的字符串。所以拆分并没有完全帮助我。我更新了第一个解决方案Hello Zbig K。感谢您的解决方案。它可以很好地获取第一行文本字符串。但问题是当我们有多个列时。通过获取行[0]的文本,它只给出所有列标题文本的字符串,并用分隔符空格连接。获取第一行所选列标题文本的任何建议。您可以尝试将结果分配给字符串,并通过拆分它们来生成数组:string headers=“1 2 3 4 5 6 7”;字符串[]str=headers.Split(“”);然后是一个很好的建议,但问题是字符串的格式。查看从结果中得到的是(调用者GCI调用者GCIOriginator默认承载Apn发起者IMS专用承载Apn),我需要的是类似于([调用者GCI],[被调用者GCI],[发起者默认承载Apn],[发起者IMS专用承载Apn])类型的字符串。因此,拆分并没有完全帮助我。我更新了第一个解决方案我编辑了html代码以更好地理解上下文。我编辑了html代码以更好地理解上下文。请您详细说明您的答案并添加一点关于您提供的解决方案的说明,好吗?因为我们知道标题始终位于索引=0上。我只是将头一行添加到函数GetAllColumnDataFromRow并检查标记。如果标签存在。将该文本存储到stringcollection中。感谢您的解决方案,Ankit。这对我来说很有吸引力。你能详细说明你的答案吗?再加上一点关于你提供的解决方案的描述?正如我们所知,标题总是在索引=0上。我只是将头一行添加到函数GetAllColumnDataFromRow并检查标记。如果标签存在。将该文本存储到stringcollection中。感谢您的解决方案,Ankit。这对我来说很有魅力。
  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;
    }