Skip to content

Instantly share code, notes, and snippets.

@monperrus
Created September 21, 2012 13:23
Show Gist options
  • Save monperrus/3761440 to your computer and use it in GitHub Desktop.
Save monperrus/3761440 to your computer and use it in GitHub Desktop.
Complex element queries with Selenium 2
WebDriver webDriver = new FirefoxDriver();
webDriver.navigate().to("http://example.com/some/page");
LinkedHashSet<RemoteWebElement> clickableElements = new LinkedHashSet<RemoteWebElement>();
LinkedHashSet<RemoteWebElement> belowInformationElements = new LinkedHashSet<RemoteWebElement>();
LinkedHashSet<RemoteWebElement> rightToLanguageElements = new LinkedHashSet<RemoteWebElement>();
LinkedHashSet<RemoteWebElement> imageElements = new LinkedHashSet<RemoteWebElement>();
LinkedHashSet<RemoteWebElement> englishElements = new LinkedHashSet<RemoteWebElement>();
// fetch all elements
//
clickableElements.add((RemoteWebElement) webDriver.findElements(By.cssSelector("a"));
clickableElements.add((RemoteWebElement) webDriver.findElements(By.cssSelector("input[type=button]"));
// [...]
// => see code comparison for "Identifying specific elements by visible element properties"
// build complex query with AND, OR and NOT
LinkedHashSet<RemoteWebElement> complexQueryElements = new LinkedHashSet<RemoteWebElement>();
for (RemoteWebElement clickable: clickableElements) {
for (RemoteWebElement belowInformation: belowInformationElements) {
// is clickable AND below information
if(clickable.getId().equals(belowInformation.getId())) {
complexQueryElements.add(belowInformation);
}
// OR
for (RemoteWebElement rightToLanguage: rightToLanguageElements) {
for (RemoteWebElement image: imageElements) {
for (RemoteWebElement english: englishElements) {
// is image AND right to language AND NOT english
if(rightToLanguage.getId().equals(image.getId()) && !rightToLanguage.getId().equals(english.getId())) {
complexQueryElements.add(image);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment