This is the code to select value from the drop down
The value for selectlocator will be the xpath or name of dropdown box, and for optionLocator will have the value to be selected from the dropdown box.
public static boolean select(final String selectLocator,
final String optionLocator) {
try {
element(selectLocator).clear();
element(selectLocator).sendKeys(Keys.PAGE_UP);
for (int k = 0; k <= new Select(element(selectLocator))
.getOptions().size() - 1; k++) {
combo1.add(element(selectLocator).getValue());
element(selectLocator).sendKeys(Keys.ARROW_DOWN);
}
if (combo1.contains(optionLocator)) {
element(selectLocator).clear();
new Select(element(selectLocator)).selectByValue(optionLocator);
combocheck = element(selectLocator).getValue();
combo = "";
return true;
} else {
element(selectLocator).clear();
combo = "The Value " + optionLocator
+ " Does Not Exist In The Combobox";
return false;
}
} catch (Exception e) {
e.printStackTrace();
errorcontrol.add(e.getMessage());
return false;
}
}
private static RenderedWebElement element(final String locator) {
try {
return (RenderedWebElement) drivers.findElement(by(locator));
} catch (Exception e) {
errorcontrol.add(e.getMessage());
return (RenderedWebElement) drivers.findElement(by(locator));
}
}
Thanks,
Rekha.