1

有没有办法计算存在的复选框总数?

这是页面生成的html源代码..

<tr> 
  <td>
    <span style="padding-left:15px;">
    <input id="ctl00_ContentPlaceHolder1_Control1_stCat_8" type="checkbox" name="ctl00$ContentPlaceHolder1$AddControl1$cat$lstCat$8"/>
    <label for="ctl00_ContentPlaceHolder1_AddControl1_lstCat_8">Item 1</label>
    </span>
 </td>
</tr>

<tr> 
  <td>
    <span style="padding-left:15px;">
    <input id="ctl00_ContentPlaceHolder1_Control1_stCat_9" type="checkbox" name="ctl00$ContentPlaceHolder1$AddControl1$cat$lstCat$8"/>
    <label for="ctl00_ContentPlaceHolder1_AddControl1_lstCat_9">Item 2</label>
    </span>
  </td>
</tr>

<tr> 
  <td>
    <span style="padding-left:15px;">
    <input id="ctl00_ContentPlaceHolder1_Control1_stCat_10" type="checkbox" name="ctl00$ContentPlaceHolder1$AddControl1$cat$lstCat$8"/>
    <label for="ctl00_ContentPlaceHolder1_AddControl1_lstCat_10">Item 3</label>
    </span>
  </td>
</tr>

在此处输入图像描述

4

3 回答 3

1

这(或类似的东西)应该可以工作。

// WARNING: Untested code. Locator syntax may be
// slightly incorrect.
// Using C#, but other languages are similar.
// Assume driver is a valid IWebDriver instance.
ReadOnlyCollection<IWebElement> checkboxes = driver.FindElements(By.CssSelector("input[type='checkbox']"));
Console.WriteLine(checkboxes.Count);
于 2012-09-18T01:18:11.130 回答
0
public int CountCheckBoxes(IWebDriver driver)
    {
        int countedCheckBoxes = driver.FindElements(By.XPath("//input[@type='checkbox']")).Count;
        return countedCheckBoxes;
    }
于 2017-08-22T11:03:28.870 回答
0
    int countedCheckBoxes = driver.FindElements(By.CssSelector("input[type='checkbox']")).Count;
于 2017-08-10T14:32:02.887 回答