我想从“可用特权”中单击多个特权,然后将其移至“分配的特权”,但是,列表在每次滚动后都会更改,因此,我一时无法检索完整的列表项。
以下是相同内容的html源。
<div id="listBoxContentsource" style="-webkit-appearance: none; background: transparent; outline: none; border: none; padding: 0px; overflow: hidden; margin: 0px; left: 0px; top: 0px; position: absolute; width: 541px; height: 198px;"><div style="outline: none 0px; overflow: hidden; width: 560px; position: relative; height: 404px;"><div role="option" id="listitem0source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 0px; left: 0px;"><span style="white-space: pre; display: block; visibility: inherit; width: 531px;" class="jqx-listitem-state-normal jqx-item jqx-rc-all"><span data-toggle="tooltip" data-content="Grants access to the Admin module on a global level, allowing users to access data which are related to all groups and individual users" id="jqxWidget1d58e406">ADM_ACC_GLOBAL</span></span></div><div role="option" id="listitem1source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 25px; left: 0px;"><span style="white-space: pre; display: block; visibility: inherit; width: 531px;" class="jqx-listitem-state-normal jqx-item jqx-rc-all"><span data-toggle="tooltip" data-content="Grants access to the Admin module on a group level, allowing users to access data which are related to the users from their own group" id="jqxWidgetf9e82031">ADM_ACC_GROUP</span></span></div><div role="option" id="listitem2source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 50px; left: 0px;"><span style="white-space: pre; display: block; visibility: inherit; width: 531px;" class="jqx-listitem-state-normal jqx-item jqx-rc-all"><span data-toggle="tooltip" data-content="Grants access to the Admin module on a user level, allowing users to access data which are related ONLY to their own account" id="jqxWidget7fd6d172">ADM_ACC_OWN</span></span></div><div role="option" id="listitem3source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 75px; left: 0px;"><span style="white-space: pre; display: block; visibility: inherit; width: 531px;" class="jqx-listitem-state-normal jqx-item jqx-rc-all"><span data-toggle="tooltip" data-content="Enable a User to edit Group's FieldTrax Virtual Number, FieldTrax Sender ID" id="jqxWidgetf6801832">ADM_OPT_GROUP_ADMIN_FT_SHOW</span></span></div><div role="option" id="listitem4source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 100px; left: 0px;"><span style="white-space: pre; display: block; visibility: inherit; width: 531px;" class="jqx-listitem-state-normal jqx-item jqx-rc-all"><span data-toggle="tooltip" data-content="This setting allows users to set how many users can be maximally assigned to a group" id="jqxWidget5073c223">ADM_OPT_GROUP_ADMIN_SET_MAX_USERS_PER_GROUP_ENABLE</span></span></div><div role="option" id="listitem5source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 125px; left: 0px;"><span style="white-space: pre; display: block; visibility: inherit; width: 531px;" class="jqx-listitem-state-normal jqx-item jqx-rc-all"><span data-toggle="tooltip" data-content="Changing quota that is assigned to group, for applying and removing " id="jqxWidgetf2a5e5ad">ADM_OPT_MANAGE_QUOTA_GROUP</span></span></div><div role="option" id="listitem6source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 150px; left: 0px;"><span style="white-space: pre; display: block; visibility: inherit; width: 531px;" class="jqx-listitem-state-normal jqx-item jqx-rc-all"><span data-toggle="tooltip" data-content="Enable a User to assign and deassign all available privileges" id="jqxWidgetc2ebc33b">ADM_OPT_ROLES_IGNORE_PRIVILEGE_ASSIGNABILITY</span></span></div><div role="option" id="listitem7source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 175px; left: 0px;"><span style="white-space: pre; display: block; visibility: inherit; width: 531px;" class="jqx-listitem-state-normal jqx-item jqx-rc-all"><span data-toggle="tooltip" data-content="Allow User to add / edit a MobileTrax Access" id="jqxWidget47252eba">ADM_OPT_USER_ADMIN_ACCESS_MT</span></span></div><div role="option" id="listitem8source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 200px; left: 0px;"><span style="white-space: pre; display: block; visibility: inherit; width: 531px;" class="jqx-listitem-state-normal jqx-item jqx-rc-all"><span data-toggle="tooltip" data-content="Allow User to add / edit a Web Access">ADM_OPT_USER_ADMIN_ACCESS_WEB</span></span></div><div role="option" id="listitem9source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 225px;"><span style="white-space: pre; visibility: hidden;" class=""></span></div><div role="option" id="listitem10source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 250px;"><span style="white-space: pre; visibility: hidden;" class=""></span></div><div role="option" id="listitem11source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 275px;"><span style="white-space: pre; visibility: hidden;" class=""></span></div><div role="option" id="listitem12source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 300px;"><span style="white-space: pre; visibility: hidden;" class=""></span></div><div role="option" id="listitem13source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 325px;"><span style="white-space: pre; visibility: hidden;" class=""></span></div><div role="option" id="listitem14source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 350px;"><span style="white-space: pre; visibility: hidden;" class=""></span></div><div role="option" id="listitem15source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 375px;"><span style="white-space: pre; visibility: hidden;" class=""></span></div><div role="option" id="listitem16source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 400px;"><span style="white-space: pre; visibility: hidden;" class=""></span></div><div role="option" id="listitem17source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 425px;"><span style="white-space: pre; visibility: hidden;" class=""></span></div><div role="option" id="listitem18source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 450px;"><span style="white-space: pre; visibility: hidden;" class=""></span></div><div role="option" id="listitem19source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 475px;"><span style="white-space: pre; visibility: hidden;" class=""></span></div></div></div>
<div role="option" id="listitem0source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 0px; left: 0px;"><span style="white-space: pre; display: block; visibility: inherit; width: 531px;" class="jqx-listitem-state-normal jqx-item jqx-rc-all"><span data-toggle="tooltip" data-content="Grants access to the Admin module on a global level, allowing users to access data which are related to all groups and individual users" id="jqxWidget1d58e406">ADM_ACC_GLOBAL</span></span></div>
<div role="option" id="listitem1source" class="jqx-listitem-element draggable jqx-draggable" style="height: 24px; top: 25px; left: 0px;"><span style="white-space: pre; display: block; visibility: inherit; width: 531px;" class="jqx-listitem-state-normal jqx-item jqx-rc-all"><span data-toggle="tooltip" data-content="Grants access to the Admin module on a group level, allowing users to access data which are related to the users from their own group" id="jqxWidgetf9e82031">ADM_ACC_GROUP</span></span></div>
到目前为止,我已完成以下操作:
WebElement ele = driver.findElement(By.id("source"));
List<WebElement> values = ele.findElements(By.xpath("//*[starts-with(@id,'listitem')]"));
for (WebElement val : values) {
System.out.println(val.getText().toString());// Prints the values of the Listbox
if(val.getText().equalsIgnoreCase("ADM_ACC_OWN"))
{
val.click();
}
答案 0 :(得分:0)
非常感谢您的所有回复。我已经解决了这个问题。
请参考以下代码:
public void selectPrivileges(String Privilege) {
WebElement listbox = driver.findElement(By.id("source"));
Set<String> roles = new LinkedHashSet<>();
int clickedEventsCount = 0;
List<WebElement> elements = listbox.findElements(By.xpath("//*[starts-with(@id,'listitem')]"));
try {
String[] privileges = Privilege.split("\\|");
while (true) {
Set<String> rolesSet = new HashSet<>();
for (WebElement element : elements) {
String role = element.getText();
if (null != role && !role.isEmpty() && role.length() > 0) {
for (String privilege : privileges) {
if (role.equalsIgnoreCase(privilege)) {
// move
element.click();
driver.findElement(By.id("add")).click();
clickedEventsCount++;
}
}
rolesSet.add(role);
}
}
if (clickedEventsCount == privileges.length) {
break;
}
driver.findElement(By.id("jqxScrollAreaDownverticalScrollBarsource")).click();
if (!roles.addAll(rolesSet)) {
break;
}
}
} catch (Exception ex) {
System.out.println(ex);
}
}
答案 1 :(得分:-1)
您是否尝试过使用硒选择?
这是使用硒选择的自定义选择方法的简化示例:
public WebElement selectOption(final String option, WebElement element) {
final Select select = new Select(element);
select.selectByVisibleText(option);
return element;
}