我想获取表格中的行数。 我已经尝试过几种方式(没有任何成功),最后我尝试了以下方式:
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']")));
在调试时,我得到Count = 1,而有3行。当然。
所有3个人都有同一个班级。
此外,此表中甚至还有一个属性:numrecords="3"
当再次使用以下内容时,我得到的值为1而不是3。
var _tableOfInterestsCount = wait.Until(x => x.FindElement(By.XPath("//*[@id='gridBodyTable']")).GetAttribute("numrecords"));
相关的HTML代码:
<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;">
<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">
<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>
</tr><tr class="ms-crm-List-Row" oid="{5943AB8E-39F7-E611-BE37-00155D47B163}" otype="10046" otypename="new_contact_content" selected="false">
<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>
</tr><tr class="ms-crm-List-Row" oid="{5A43AB8E-39F7-E611-BE37-00155D47B163}" otype="10046" otypename="new_contact_content" selected="false">
<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>
</tr></tbody>
</table>
&#13;
编辑: 顺便说一下,当有2行时,计数为0。 我甚至尝试了以下方法,但仍然得到错误的属性值
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 :(得分:1)
为什么在有ID的情况下通过xpath访问你的表? 你通常应该这样做:
var _tableOfInterestsCount =Driver.FindElement(By.Id("gridBodyTable"));
List<IWebElement> _trs = _tableOfInterestsCount .FindElements(By.ClassName("ms-crm-List-Row")).ToList();
int count=_trs.Count();
如果这不起作用,请同时共享行HTML。
答案 1 :(得分:0)
你尝试过这样的事吗?
findElements(By.xpath(".//div/table/tbody/tr")).size()
答案 2 :(得分:0)
不幸的是,问题是我必须先切换到不同的帧,然后找到元素。
_webdriverIE.SwitchTo().Frame(_webdriverIE.FindElement(By.XPath(".//iframe[@id='area_new_contact_new_contact_contentFrame']")));