使用For循环中的WebElement索引遍历HTML表中的'tr'行

时间:2018-09-25 19:36:36

标签: c# selenium-webdriver

我试图遍历表中的行,从每行的第二个“ td”中提取文本,并将其与列表中的值进行比较。尽管索引号在增加,但是当用作使用'FindElements'找到的'tr'行的索引号时,它不会移至下一行,因此每次都会从第一行返回文本。

List<string> policyList  = parsePolicyTable(driver, pageaction);

int numofPolicies        = policyList.Count();

for (var polListIndex = 0; polListIndex < numofPolicies; polListIndex++)
{
    By polTableBy        = ObjRepository("PolicySearch", "PolicyTable");
    IWebElement polTable = driver.FindElement(polTableBy);

    var rows             = polTable.FindElements(By.TagName("tr"));
    int rowNum           = rows.Count();

    for (var searchResultRow = 0; searchResultRow < rowNum; searchResultRow++)
    {
        //IWebElement polNumRow  = rows.ElementAt(searchResultRow);
        IWebElement polNumElem = rows[searchResultRow].FindElement(By.XPath("//td[2]"));

        string polNum          = polNumElem.Text;
        string listPolNum      = policyList[polListIndex];

        WritetoLogFile("Policy Number [" + searchResultRow + "] from Search - " + polNum + " | Policy Number [" + polListIndex + "] from List - " + listPolNum);

输出:

  

搜索中的策略编号[0]-XAL0001046-2 |列表中的策略编号[0]-XAL0001028-0

     

搜索中的政策编号[1]-XAL0001046-2 |列表中的策略编号[0]-XAL0001028-0

     

搜索中的政策编号[2]-XAL0001046-2 |列表中的策略编号[0]-XAL0001028-0

我也尝试过,上面已经注释掉了,使用具有相同结果的.ElementAt函数定位下一行。

我想念什么?

HTML ...

      <tr class="odd">
    <td title="Click to select this policy">
      <input type="radio" name="Policy" id="Policy1039" value="1039">
    </td>
    <td>
      <span class="link8pt cppsInfo" title="XAL0001039-7" data-policynumber="XAL0001039-7">XAL0001039-7</span>
    </td>
    <td title="Policyholder" style="white-space: nowrap;">SHOREY, NORMAND</td>
    <td title="Policyholder Phone Number(s)" style="white-space: nowrap;">(216) 657-7668 (H)</td>
    <td title="Policyholder Address">13675 HIGHWAY 43<br>RUSSELLVILLE, AL 35653-2831</td>
    <td>
      <span class="linkCompact agencyInfo" title="TEST AGENCY #1" data-agencyid="9000">9000-TEST AGENCY #1</span>
    </td>
    <td title="Policy Issue Date">08/31/2018</td>
    <td title="Policy Expiration Date">03/01/2019</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td title="Email Address"></td>
    <td title="Policy Term">6 months paid in full</td>
  </tr>
  <tr class="even">
    <td title="Click to select this policy">
      <input type="radio" name="Policy" id="Policy1037" value="1037">
    </td>
    <td>
      <span class="link8pt cppsInfo" title="XAL0001037-1" data-policynumber="XAL0001037-1">XAL0001037-1</span>
    </td>
    <td title="Policyholder" style="white-space: nowrap;">SMITH, JAMES</td>
    <td title="Policyholder Phone Number(s)" style="white-space: nowrap;">(205) 441-4444 (C)</td>
    <td title="Policyholder Address">100 HUEYTOWN ROAD<br>HUEYTOWN, AL 35023</td>
    <td>
      <span class="linkCompact agencyInfo" title="TEST AGENCY #1" data-agencyid="9000">9000-TEST AGENCY #1</span>
    </td>
    <td title="Policy Issue Date">08/30/2018</td>
    <td title="Policy Expiration Date">03/01/2019</td>
    <td class="xmark" title="This policy has a cancellation notice">X</td>
    <td>&nbsp;</td>
    <td title="Email Address"></td>
    <td title="Policy Term">6 month/5 pay - 17% down</td>
  </tr>

1 个答案:

答案 0 :(得分:0)

从表面上看,这对我来说很好,所以我很冒险。但是后来我相信FindElements返回一个IReadOnlyCollection。

一个纯粹的猜测,您能尝试一下吗?

var rows = polTable.FindElements(By.TagName("tr")).ToList();

不过,实际上,调试将是您的朋友。查看行的元素是什么,等等。