2014-04-15 176 views
0

這是下拉的圖片的鏈接: http://i.stack.imgur.com/tQs4Z.png硒的webdriver(2.0) - 多選擇[複選框]下拉菜單 -

我一直在研究這個了一下,但一直未能找到一個合適的解下拉列表中可以選中複選框。多選可能。下面是下拉菜單的HTML,其中包含「必填字段」星號和下拉菜單的標題。抱歉格式化出來時髦。

我相信如果它是一個Select元素會更容易,但是因爲它是一個Input,所以我想伸手去看看處理這些元素的最佳方式。我應該提到,我正在使用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> 
+0

從圖像看,它看起來像是在工作。是否有例外被提出? –

+0

問題是如何使用Selenium WebDriver處理這個菜單。 – Scott

回答

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 {} 
+0

對於那些閱讀代碼,它會檢查下拉列表中的所有框。 – Scott

0

這是一個動態html。雖然看起來像下降,但事實並非如此。它只是複選框的列表。

如果你想請根據市複選框,做如下

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 

我沒有硒設置現在寫代碼你...但這個邏輯應該工作

+0

非常感謝。我將編寫一些代碼,並在工作時檢查你的答案! – Scott

+0

仍然無法讓它工作。可能做錯了。任何人都有上述邏輯的代碼翻譯? – Scott