对于我的项目,我有一个包含不同复选框的下拉菜单。默认情况下,某些复选框处于选中状态。我想选择和取消选择复选框,获取文本值并验证是否保存了选中的值。 我必须使用硒。
下面是包含下拉列表和复选框的div
<div class="iframe-multi-dropdown multisection-dropdown outline-none1">
<ul class="top multiSelect" id="g-dropdown" data-type="multiSelect" autoid="dropdown-values">
<li id="li-0" data-level-id="" class="li-level1" tabindex="-1">
<h3 data-id="" data-acc-id="" id="li-0-h" data-level-header="level1-header" class="header level1-header clearfix Paragraph1 selected outline-none1 label-css-level1 selectable " role="presentation">
<input type="checkbox" aria-label1="" aria-hidden="true" name="level1" value="">
<span role="checkbox" aria-label="Rupendra,E@Reddy" tabindex="0" aria-checked="true" class="component-check-box yodlee-font-icon svg_tick outline-none1 check-box-css level1"></span>
<span class="ellipsis name text text-css-level1" aria-hidden="true" title="Rupendra,E@Reddy" data-title="Rupendra,E@Reddy">Rupendra,E@Reddy</span>
</h3>
</li>
<li id="li-1" data-level-id="" class="li-level1" tabindex="-1">
<h3 data-id="" id="li-1-h" data-acc-id="" data-level-header="level1-header" class=" header level1-header Paragraph1 clearfix outline-none1 label-css-level1 selectable " role="presentation">
<input type="checkbox" aria-label1="" aria-hidden="true" name="level1" value="">
<span role="checkbox" aria-label="Chandra,K" tabindex="0" aria-checked="false" class="component-check-box yodlee-font-icon outline-none1 check-box-css level1"></span>
<span class="ellipsis name text text-css-level1" aria-hidden="true" title="Chandra,K" data-title="Chandra,K">Chandra,K</span>
</h3>
</li>
<li id="li-2" data-level-id="" class="li-level1" tabindex="-1">
<h3 data-id="" id="li-2-h" data-acc-id="" data-level-header="level1-header" class=" header level1-header Paragraph1 clearfix outline-none1 label-css-level1 selectable " role="presentation">
<input type="checkbox" aria-label1="" aria-hidden="true" name="level1" value="">
<span role="checkbox" aria-label="Deven,S" tabindex="0" aria-checked="false" class="component-check-box yodlee-font-icon outline-none1 check-box-css level1"></span>
<span class="ellipsis name text text-css-level1" aria-hidden="true" title="Deven,S" data-title="Deven,S">Deven,S</span>
</h3>
</li>
<li id="li-3" data-level-id="" class="li-level1" tabindex="-1">
<h3 data-id="" data-acc-id="" id="li-3-h" data-level-header="level1-header" class="header level1-header clearfix Paragraph1 selected outline-none1 label-css-level1 selectable " role="presentation">
<input type="checkbox" aria-label1="" aria-hidden="true" name="level1" value="">
<span role="checkbox" aria-label="Mohan && Raj" tabindex="0" aria-checked="true" class="component-check-box yodlee-font-icon svg_tick outline-none1 check-box-css level1"></span>
<span class="ellipsis name text text-css-level1" aria-hidden="true" title="Mohan && Raj" data-title="Mohan && Raj">Mohan && Raj</span>
</h3>
</li>
<li id="li-4" data-level-id="" class="li-level1" tabindex="-1">
<h3 data-id="" data-acc-id="" id="li-4-h" data-level-header="level1-header" class="header level1-header clearfix Paragraph1 selected outline-none1 label-css-level1 selectable " role="presentation">
<input type="checkbox" aria-label1="" aria-hidden="true" name="level1" value="">
<span role="checkbox" aria-label="Yumiko<>K" tabindex="0" aria-checked="true" class="component-check-box yodlee-font-icon svg_tick outline-none1 check-box-css level1"></span>
<span class="ellipsis name text text-css-level1" aria-hidden="true" title="Yumiko<>K" data-title="Yumiko<>K">Yumiko<>K</span>
</h3>
</li>
</ul>
<ul class="no-records hide">
<li id="test" class="li-level1" role="alert" aria-atomic="true" aria-live="assertive">
<div data-id="test" tabindex="0" class=" header level1-header Paragraph1 outline-none1 label-css-level1">
<span class="ellipsis name text text-css-level1"></span>
</div>
</li>
</ul>
</div>
我正在使用以下通用代码:
public List<String> checkUncheck() {
LinkedList<String> owner = new LinkedList<String>();
List<WebElement> li = SeleniumUtil.getWebElements(d, "checkUncheck", pageName, frameName);
int si =li.size();
System.out.println(si);
for ( WebElement els : li ) {
String values =els.getText();
if (!els.isSelected()) {
els.click();
owner.add(els.getText().trim());
//logger.error("Could not able to get the owner name checkbox to click");
}
if (els.isSelected()) {
els.click();
}
}
return owner;
}
并在下面调用它
logger.info("Check and Uncheck the checkboxes");
List<String> ownerName = manageAccount_loc.checkUncheck();
logger.info("The value selected in Pop up are " + ownerName);
它正在打印所有五个名称,但我只需要选择的名称
答案 0 :(得分:0)
这就是我对您的要求
由于它不是传统的选择框,而是HTML-JS实现,所以最好使用以下代码
public List<String> checkUncheck() {
LinkedList<String> owner = new LinkedList<String>();
List<WebElement> liElementList = SeleniumUtil.getWebElements(d, "checkUncheck", pageName, frameName);
int listSize = liElementList.size();
System.out.println(listSize);
for ( WebElement liElement : liElementList) {
// Find h3 tag which is part of li.
WebElement h3Element = liElement.findElement(By.tagName("selected"));
// h3 element has a class "selected" which marks if an element
// is selected. We use this to determine if the checkbox is selected
boolean isSelected = h3Element.getAttribute("class").contains("selected") ? true : false;
if (isSelected)
owner.add(liElement.getText().trim());
// Click liElement regardless of selected status
liElement.click();
}
return owner;
}