Selenium WebDriver(2.0) - 多选[复选框]下拉菜单 -

时间:2014-04-15 14:38:37

标签: c# .net selenium selenium-webdriver webdriver

这是下拉列表图片的链接: http://i.stack.imgur.com/tQs4Z.png

我一直在研究这个问题,但一直无法找到合适的解决方案。下拉列表中有一个可以检查的复选框列表。可以进行多种选择。下面是下拉列表的HTML,其中包含“必填字段”星号和下拉列表的标题。很抱歉格式化它是时髦的。

我相信如果它是一个Select元素会更容易,但由于它是一个输入,我想伸出手去看看处理这些元素的最佳方法。我应该提一下,我正在使用WebDriver for MS Visual Studio 2010 C#。

此外,我发现this虽然相关,但似乎没有使用复选框下拉列表。

<tr>
    <td align="right" valign="top" style="background-color:White;">
        <span id="ctl03_ctl00_ctl00_Tabbed_ContentPlaceHolder_ContentPlaceHolder1_ContentPlaceHolder1_ctl00_ctl00_gvQuestions_ctl03_lblRequired" class="RequiredIndicator">*</span>
    </td>
    <td valign="top" style="background-color:White;">2</td>
    <td valign="top" style="background-color:White;">Question 2 Cities?</td>
    <td align="left" valign="top" style="border-color:White;border-width:1px;border-style:Solid;">
        <input type="hidden" name="ctl03$ctl00$ctl00$Tabbed_ContentPlaceHolder$ContentPlaceHolder1$ContentPlaceHolder1$ctl00$ctl00$gvQuestions$ctl03$hdnQId" id="ctl03_ctl00_ctl00_Tabbed_ContentPlaceHolder_ContentPlaceHolder1_ContentPlaceHolder1_ctl00_ctl00_gvQuestions_ctl03_hdnQId" value="2">
        <input type="hidden" name="ctl03$ctl00$ctl00$Tabbed_ContentPlaceHolder$ContentPlaceHolder1$ContentPlaceHolder1$ctl00$ctl00$gvQuestions$ctl03$hdnVId" id="ctl03_ctl00_ctl00_Tabbed_ContentPlaceHolder_ContentPlaceHolder1_ContentPlaceHolder1_ctl00_ctl00_gvQuestions_ctl03_hdnVId" value="3">
        <input type="hidden" name="ctl03$ctl00$ctl00$Tabbed_ContentPlaceHolder$ContentPlaceHolder1$ContentPlaceHolder1$ctl00$ctl00$gvQuestions$ctl03$hdnAId" id="ctl03_ctl00_ctl00_Tabbed_ContentPlaceHolder_ContentPlaceHolder1_ContentPlaceHolder1_ctl00_ctl00_gvQuestions_ctl03_hdnAId" value="3">
        <div id="ctl03_ctl00_ctl00_Tabbed_ContentPlaceHolder_ContentPlaceHolder1_ContentPlaceHolder1_ctl00_ctl00_gvQuestions_ctl03_cblListItems2" class="RadComboBox RadComboBox_Classic" style="width:150px;white-space:normal;">
            <table summary="combobox" border="0" style="border-width: 0px; border-collapse: collapse; width: 150px;">
                <tbody>
                    <tr>
                        <td class="rcbInputCell rcbInputCellLeft" style="width:100%;">
                            <input name="ctl03$ctl00$ctl00$Tabbed_ContentPlaceHolder$ContentPlaceHolder1$ContentPlaceHolder1$ctl00$ctl00$gvQuestions$ctl03$cblListItems2" type="text" class="rcbInput radPreventDecorate" id="ctl03_ctl00_ctl00_Tabbed_ContentPlaceHolder_ContentPlaceHolder1_ContentPlaceHolder1_ctl00_ctl00_gvQuestions_ctl03_cblListItems2_Input" value="" autocomplete="off">
                        </td>
                        <td class="rcbArrowCell rcbArrowCellRight">
                            <a id="ctl03_ctl00_ctl00_Tabbed_ContentPlaceHolder_ContentPlaceHolder1_ContentPlaceHolder1_ctl00_ctl00_gvQuestions_ctl03_cblListItems2_Arrow" style="overflow: hidden;display: block;position: relative;outline: none;">select</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="rcbSlide" style="z-index:6000;">
                <div id="ctl03_ctl00_ctl00_Tabbed_ContentPlaceHolder_ContentPlaceHolder1_ContentPlaceHolder1_ctl00_ctl00_gvQuestions_ctl03_cblListItems2_DropDown" class="RadComboBoxDropDown RadComboBoxDropDown_Classic " style="display:none;">
                    <div class="rcbScroll rcbWidth" style="width:100%;">
                        <ul class="rcbList" style="list-style:none;margin:0;padding:0;zoom:1;">
                            <li class="rcbItem"><label><input type="checkbox" class="rcbCheckBox">Denver</label></li>
                            <li class="rcbItem"><label><input type="checkbox" class="rcbCheckBox">Los Angeles</label></li>
                            <li class="rcbItem"><label><input type="checkbox" class="rcbCheckBox">New York City</label></li>
                        </ul>
                    </div>
                </div>
            </div>
            <input id="ctl03_ctl00_ctl00_Tabbed_ContentPlaceHolder_ContentPlaceHolder1_ContentPlaceHolder1_ctl00_ctl00_gvQuestions_ctl03_cblListItems2_ClientState" 
    name="ctl03_ctl00_ctl00_Tabbed_ContentPlaceHolder_ContentPlaceHolder1_ContentPlaceHolder1_ctl00_ctl00_gvQuestions_ctl03_cblListItems2_ClientState" 
    type="hidden" autocomplete="off" value={"logEntries":[],"value":"292","text":"","enabled":true,"checkedIndices":[],"checkedItemsTextOverflows":false}>
        </div>
    </td>
</tr>

2 个答案:

答案 0 :(得分:0)

这是一个动态的HTML。虽然看起来像下降,但事实并非如此。它只是复选框列表。

如果您想根据城市检查CheckBox,请按照

进行操作
1) select all elements by class name "rcbItem" which is LI
2) now Iterate each element, and Read "Text" property
3) if the Text property is equals to your target city (which needs to be selected)
4) Select child element by class name "rcbCheckBox" using current element (i.e. LI)
5) and set the value attribute

我现在没有Selenium设置为你编写代码......但是这个逻辑应该可行

答案 1 :(得分:0)

以下是我处理此列表的方式。模仿@CreativeManix的逻辑。

IWebElement ddl = driver.FindElement(By.Id("ctl03_ctl00_ctl00_Tabbed_ContentPlaceHolder_ContentPlaceHolder1_ContentPlaceHolder1_ctl00_ctl00_gvQuestions_ctl03_cblListItems2_Arrow"));
ddl.Click();
IList<IWebElement> lis = driver.FindElements(By.ClassName("rcbItem"));
foreach (IWebElement li in lis)
    try
    {
        IWebElement checkBox = li.FindElement(By.ClassName("rcbCheckBox"));
        //if (checkBox.Selected)
        checkBox.Click();
        break;
    }
    catch {}