WebElement select = driver.findElement(By.name("myselect")); Select dropDown = new Select(select); String selected = dropDown.getFirstSelectedOption().getText(); if(selected.equals(valueToSelect)){ //already selected; //do stuff } List<WebElement> Options = dropDown.getOptions(); for(WebElement option:Options){ if(option.getText().equals(valueToSelect)) { option.click(); //select option here; } }
/** * Returns all options belonging to this select tag. * * @return A list of {@code WebElements} representing options. */ public List<WebElement> getOptions() { return getSelect().getOptions(); }
/** * @return All selected options belonging to this select tag */ public List<WebElement> getAllSelectedOptions() { List<WebElement> toReturn = new ArrayList<WebElement>(); for (WebElement option : getOptions()) { if (option.isSelected()) { toReturn.add(option); } } return toReturn; }
public List<String> getAllValues(Supplier<By> by) { return new Select(findElement(by)).getOptions() .stream() .map(option -> option.getAttribute(VALUE)) .collect(Collectors.toList()); }
public boolean isTextPresentInTheDropdown(Supplier<By> by, String text) { final Element element = untilFound(by); Select dropdown = new Select(element); return dropdown .getOptions() .parallelStream() .anyMatch(opt -> opt.getText() .contains(text)); }
Select select = new Select(element); Actions builder = new Actions(driver); builder.keyDown(Keys.CONTROL) .click(select.getOptions().get(2)) .keyUp(Keys.CONTROL); builder.build().perform();
@Override public MultiSelect selectAll() { List<WebElement> selectOptions = this.getWrappedSelect().getOptions(); for (WebElement option : selectOptions) if (!option.isSelected()) option.click(); return this; }
public static boolean optionWithIndexIsDeselected(int index, WebElement webElement) { List<WebElement> options = new Select(webElement).getOptions(); try { return isDeselected(options.get(index)); } catch (IndexOutOfBoundsException e) { return false; } }
public static boolean hasOptionWithValue(String value, WebElement webElement) { List<WebElement> options = new Select(webElement).getOptions(); for (WebElement option : options) { if (valueEquals(value, option)) { return true; } } return false; }
WebDriverWait wait = new WebDriverWait(driver, 20); int size = 3; for (int i = 2 ; i < size ; ++i) { WebElement dropdown = driver.findElement(...); Select select = new Select(dropdown); size = select.getOptions().size(); //change the condition to the number of options select.selectByIndex(i); driver.findElement(...).click(); //download the report wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ReportViewerControl_AsyncWait_Wait"))); //wait for the loader to appear wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("ReportViewerControl_AsyncWait_Wait"))); //wait for the loader to disappear }
public Integer getFirstSelectedIndex(Supplier<By> by) { try { Select select = new Select(findElement(by)); List<WebElement> options = select.getOptions(); if (!options.isEmpty()) { return options.indexOf(select.getFirstSelectedOption()); } return null; } catch (NoSuchElementException e) { return null; } }
public static boolean hasOption(String text, WebElement webElement) { List<WebElement> options = new Select(webElement).getOptions(); for (WebElement option : options) { if (textEquals(text, option)) { return true; } } return false; }
/** * @return The first selected option in this select tag (or the currently selected option in a * normal select) */ public WebElement getFirstSelectedOption() { for (WebElement option : getOptions()) { if (option.isSelected()) { return option; } } throw new NoSuchElementException("No options are selected"); }
/** * @return The first selected option in this select tag (or the currently selected option in a * normal select) */ public WebElement getFirstSelectedOption() { for (WebElement option : getOptions()) { if (option.isSelected()) { return option; } } throw new NoSuchElementException("No options are selected"); }
public static boolean optionIsEnabled(String text, WebElement webElement) { List<WebElement> options = new Select(webElement).getOptions(); for (WebElement option : options) { if (textEquals(text, option) && isEnabled(option)) { return true; } } return false; }
public static boolean optionIsDisabled(String text, WebElement webElement) { List<WebElement> options = new Select(webElement).getOptions(); for (WebElement option : options) { if (textEquals(text, option) && isDisabled(option)) { return true; } } return false; }
public static boolean optionIsDeselected(String text, WebElement webElement) { List<WebElement> options = new Select(webElement).getOptions(); for (WebElement option : options) { if (textEquals(text, option) && isDeselected(option)) { return true; } } return false; }
public static boolean optionIsEnabled(String text, WebElement webElement) { List<WebElement> options = new Select(webElement).getOptions(); for (WebElement option : options) { if (textEquals(text, option) && isEnabled(option)) { return true; } } return false; }
public static boolean optionIsDisabled(String text, WebElement webElement) { List<WebElement> options = new Select(webElement).getOptions(); for (WebElement option : options) { if (textEquals(text, option) && isDisabled(option)) { return true; } } return false; }
public String moveThread() { clickOnMoreButton(); clickOnMoveThreadButton(); wait.forElementVisible(moveThreadModalSelectElement); Select dropList = new Select(moveThreadModalSelectElement); String selectedItem = dropList.getOptions().get(1).getText(); dropList.selectByIndex(1); wait.forElementClickable(moveThreadModalMoveThreadButton); scrollAndClick(moveThreadModalMoveThreadButton); Log.log("moveThread", "thread moved to the following board: " + selectedItem, true, driver); return selectedItem; }