2017-02-22 77 views
0

我想获得表格中的行数。 我已经在几个方面尝试过(没有成功),最后我已经尝试过通过以下方式:获取表格中的行数

var _tableOfInterestsCount = wait.Until(x => x.FindElements(By.XPath("//*[@id='gridBodyTable']/tbody"))); 
     var _trs = wait.Until(x => x.FindElements(By.XPath(".//tr[@class = 'ms-crm-List-Row']"))); 

在调试时,我得到计数​​= 1,同时有3行。当然。 所有3 tr都有相同的类。 此外,甚至还有在这个表中的属性说:numrecords="3" 并用下面的时候,再一次,我越来越而不是3

var _tableOfInterestsCount = wait.Until(x => x.FindElement(By.XPath("//*[@id='gridBodyTable']")).GetAttribute("numrecords")); 

相关的HTML代码的值为1:

<table class="ms-crm-List-Data" cellspacing="0" cellpadding="1" rules="rows" morerecords="0" totalrecordcount="3" allrecordscounted="1" oname="10046" numrecords="3" tabindex="0" primaryfieldname="new_name" summary="foo" border="1" id="gridBodyTable" style="border-style:None;border-collapse:collapse;"> 
 
\t \t <colgroup><col width="18px" class="ms-crm-List-CheckBoxColumn"><col width="302" name="new_name" class="ms-crm-List-DataColumn ms-crm-List-SortedColumn"><col width="127" name="createdon" class="ms-crm-List-DataColumn"><col></colgroup><thead><tr class="ms-crm-Hidden-List"><th scope="col" class="ms-crm-Hidden-List"></th><th scope="col" class="ms-crm-Hidden-List">שם</th><th scope="col" class="ms-crm-Hidden-List">created at</th></tr></thead><tbody><tr class="ms-crm-List-Row" oid="{5843AB8E-39F7-E611-BE37-00155D47B163}" otype="10046" otypename="new_contact_content" selected="false"> 
 
\t \t \t <td class="ms-crm-List-NonDataCell" align="center"><input type="checkbox" class="ms-crm-RowCheckBox" id="checkBox_{5843AB8E-39F7-E611-BE37-00155D47B163}" tabindex="0" title="1" style=" "></td><td class="ms-crm-List-DataCell inner-grid-cellPadding"><nobr class="gridcellpadding" title="name1"><a href="#" id="gridBodyTable_primaryField_{5843AB8E-39F7-E611-BE37-00155D47B163}_0" target="_self" title="name 1" class="ms-crm-List-Link" tabindex="0">name 1</a></nobr></td><td class="ms-crm-List-DataCell inner-grid-cellPadding ms-crm-NumbersAndDates"><nobr class="gridcellpadding" title="20/02/2017 08:55">20/02/2017 08:55</nobr></td><td class="ms-crm-List-DataCell"><nobr class="gridcellpadding"></nobr></td> 
 
\t \t </tr><tr class="ms-crm-List-Row" oid="{5943AB8E-39F7-E611-BE37-00155D47B163}" otype="10046" otypename="new_contact_content" selected="false"> 
 
\t \t \t <td class="ms-crm-List-NonDataCell" align="center"><input type="checkbox" class="ms-crm-RowCheckBox" id="checkBox_{5943AB8E-39F7-E611-BE37-00155D47B163}" tabindex="0" title="3" style=" "></td><td class="ms-crm-List-DataCell inner-grid-cellPadding"><nobr class="gridcellpadding" title="3"><a href="#" id="gridBodyTable_primaryField_{5943AB8E-39F7-E611-BE37-00155D47B163}_1" target="_self" title="3" class="ms-crm-List-Link" tabindex="0">3</a></nobr></td><td class="ms-crm-List-DataCell inner-grid-cellPadding ms-crm-NumbersAndDates"><nobr class="gridcellpadding" title="20/02/2017 08:55">20/02/2017 08:55</nobr></td><td class="ms-crm-List-DataCell"><nobr class="gridcellpadding"></nobr></td> 
 
\t \t </tr><tr class="ms-crm-List-Row" oid="{5A43AB8E-39F7-E611-BE37-00155D47B163}" otype="10046" otypename="new_contact_content" selected="false"> 
 
\t \t \t <td class="ms-crm-List-NonDataCell" align="center"><input type="checkbox" class="ms-crm-RowCheckBox" id="checkBox_{5A43AB8E-39F7-E611-BE37-00155D47B163}" tabindex="0" title="9" style=" "></td><td class="ms-crm-List-DataCell inner-grid-cellPadding"><nobr class="gridcellpadding" title="9"><a href="#" id="gridBodyTable_primaryField_{5A43AB8E-39F7-E611-BE37-00155D47B163}_2" target="_self" title="9" class="ms-crm-List-Link" tabindex="0">9</a></nobr></td><td class="ms-crm-List-DataCell inner-grid-cellPadding ms-crm-NumbersAndDates"><nobr class="gridcellpadding">20/02/2017 08:55</nobr></td><td class="ms-crm-List-DataCell"><nobr class="gridcellpadding"></nobr></td> 
 
\t \t </tr></tbody> 
 
\t </table>

编辑: 顺便说一句,当有2行,计数为0。 我甚至尝试以下,但仍然得到错误的adttribute的值:

Dictionary<string, object> attributes = executor.ExecuteScript("var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;", _tableOfInterestsCount) as Dictionary<string, object>; 
+0

你能分享相关的HTML吗? –

+0

numrecords =“3”与GetAttribute(“numberrecords”),应该是不同的? – drw85

+0

@ drw85对不起,修复它。它应该是numrecords,但它不是问题:) –

回答

1

为什么你通过XPath访问你的表时,它有一个ID? 你平时应该做喜欢的事:

var _tableOfInterestsCount =Driver.FindElement(By.Id("gridBodyTable")); 
List<IWebElement> _trs = _tableOfInterestsCount .FindElements(By.ClassName("ms-crm-List-Row")).ToList(); 
int count=_trs.Count(); 

如果这不起作用,请也共享行HTML。

+0

我已经尝试过ID以及..提供的解决方案是我的第4次。我也会分享这些行 –

+0

我编辑了HTML并提供了一个完整的。 –

+0

你确定你使用的是相同的代码吗?你可以尝试复制粘贴吗?它应该工作 –

0

你有没有尝试过这样的事情?

findElements(By.xpath(".//div/table/tbody/tr")).size() 
+0

是的。遗憾的是,计数仍然是1。 –

0

不幸的是,问题是我不得不先切换到不同的帧,然后找到元素。

_webdriverIE.SwitchTo().Frame(_webdriverIE.FindElement(By.XPath(".//iframe[@id='area_new_contact_new_contact_contentFrame']")));