2017-02-22 42 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']")));