根据HTML展开下拉菜单后如何单击复选框

时间:2018-07-26 01:02:17

标签: java selenium selenium-webdriver xpath webdriver

**HTML:**

<div id="ctl05_ctl03_cblUser" class="RadComboBox RadComboBox_Silk" 
style="width:250px;white-space:normal;"> 
<table summary="combobox" border="0" style="border-width:0;border- 
collapse:collapse;width:100%" class="">
<tbody>
<tr>
<td class="rcbInputCell rcbInputCellLeft" style="width:100%;">
<input name="ctl05$ctl03$cblUser" type="text" class="rcbInput 
 radPreventDecorate" id="ctl05_ctl03_cblUser_Input" value="" autocomplete="off">
</td>
<td class="rcbArrowCell rcbArrowCellRight">
<a id="ctl05_ctl03_cblUser_Arrow" style="overflow: hidden;display: 
block;position: relative;outline: none;">select
</a>
</td> 
</tr>
</tbody>
</table>
<input id="ctl05_ctl03_cblUser_ClientState" 
 name="ctl05_ctl03_cblUser_ClientState" type="hidden" autocomplete="off">
 </div>
 <div class="rcbSlide" style="z-index: 6000; display: block; width: 250px; 
  top: 362.594px; left: 247px; overflow: visible;">
<div id="ctl05_ctl03_cblUser_DropDown" class="RadComboBoxDropDown 
  RadComboBoxDropDown_Silk " style="width: 248px; display: block; top: 0px; 
    visibility: visible; transition: none;">
<div class="rcbScroll rcbWidth" style="height: 345px;">
<ul class="rcbList">
<li class="rcbItem">
<label>
<input type="checkbox" class="rcbCheckBox">John, Smith</label>: :after
 </li>
 <li class="rcbItem"><label>
 <input type="checkbox" class="rcbCheckBox">Jane, Dow</label>: :after
 </li>
 </ul>
 </div>
</div>

在此处添加了注释的代码,以供进一步分析。 driver.findElement(By.xpath(“ // div [@ class ='RadComboBoxDropDown RadComboBoxDropDown_Silk'] [包含(@id,'_ cblUser_DropDown')] // ul [@ class ='rcbList'] // label [包含(。 ,'John,Smith')] / input [@ class ='rcbCheckBox']“))。click();

4 个答案:

答案 0 :(得分:0)

您可以使用下面的xpath选中该复选框。我已指定示例根据名称(Jane,Dev)选择该复选框

Xpath: //div[@class='rcbSlide']//ul[@class='rcbList']//*[contains(text(),'Jane, Dev')]/input

driver.findElement(By.xpath("//div[@class='rcbSlide']//ul[@class='rcbList']//*[contains(text(),'Jane, Dev')]/input")).click();

答案 1 :(得分:0)

对我有用。我认为您的逻辑没有错。您可以等待元素可单击,然后再单击它。

 WebElement ddl = driver.findElement(By.id("ctl05_ctl03_cblUser_Arrow"));
 ddl.click();
 WebDriverWait wait = new WebDriverWait(driver, 60);
 WebElement lis = driver.findElement(By.className("rcbItem"));
 WebElement checkBox = wait.until(ExpectedConditions.elementToBeClickable(lis.findElement(By.className("rcbCheckBox")))); 
 checkBox.click();    

答案 2 :(得分:0)

根据您在复选框上与click()共享的 HTML ,文字为 John,Smith ,您可以使用以下解决方案:

driver.findElement(By.xpath("//td[@class='rcbInputCell rcbInputCellLeft']/input[@class='rcbInput radPreventDecorate'][contains(@id,'User_Input')]")).click();
driver.findElement(By.xpath("//div[@class='RadComboBoxDropDown RadComboBoxDropDown_Silk'][contains(@id,'_cblUser_DropDown')]//ul[@class='rcbList']//label[contains(.,'John, Smith')]/input[@class='rcbCheckBox']")).click();

答案 3 :(得分:0)

在单击下拉菜单选项之前,请确保您有足够的等待时间使下拉菜单选项可见。您可以尝试几个选项。 对于简,陶氏

driver.findElement(By.cssSelector("li#rcbItem>label>input")).click();

driver.findElement(By.xpath("//li[@class='rcbItem']/label/input")).click();

//if you know the text
driver.findElement(By.xpath("//input[text()='Jane, Dow']")).click();