Skip to content

Instantly share code, notes, and snippets.

@seleniumgists
Created August 14, 2020 06:06
Show Gist options
  • Save seleniumgists/d8661b9cc6d17a27e00a5d9de0a33749 to your computer and use it in GitHub Desktop.
Save seleniumgists/d8661b9cc6d17a27e00a5d9de0a33749 to your computer and use it in GitHub Desktop.
generated automatically from #selenium on seleniumhq slack
// The id of the checkbox we intend to click.
final String checkboxIdToClick = "363171428_2399318203";
// Check values of the map for a webelement that has a matching id attribute.
getCheckboxMap().forEach((k,v) -> {
// If the value's attribute matches expected id, then the key webelement should be clicked since the child cannot.
if(v.getAttribute("id").equals(checkboxIdToClick) k.click()
});
private Map<WebElement,WebElement> getCheckboxMap() {
// Strings to hold css targets.
final String containerCss = "div.checkbox-container";
final String checkboxCss = containerCss + " input";
// Create invidual lists of parent and child input elements.
List<WebElement> elementContainers = driver.findElements(containerCss);
List<WebElement> checkboxElements = driver.findElements(checkboxCss);
final Map map = new Map();
// make sure the lists are the same size before iterating.
if(elementContainers.size() == checkboxElements.size()){
// Iterate through both lists and add them to a map.
// Key = parent div element.
// Value = child input element.
for(int i = 0; i < elementContainers.size(); i++){
map.put(elementContainers.get(i),checkboxElements.get(i));
}
}
return map;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment