Refine search
WebClient client = ... Page page = client.getPage(url); HtmlSelect select = (HtmlSelect) page.getElementById(mySelectId); HtmlOption option = select.getOptionByValue(desiredOptionValue); select.setSelectedAttribute(option, true);
/** * Changes the number of options: removes options if the new length * is less than the current one else add new empty options to reach the * new length. * @param newLength the new length property value */ public void jsxSet_length(final int newLength) { final int currentLength = htmlSelect_.getOptionSize(); if (currentLength > newLength) { htmlSelect_.setOptionSize(newLength); } else { for (int i = currentLength; i < newLength; i++) { final HtmlOption option = (HtmlOption) HTMLParser.getFactory(HtmlOption.TAG_NAME).createElement( htmlSelect_.getPage(), HtmlOption.TAG_NAME, null); htmlSelect_.appendOption(option); if (!getBrowserVersion().isIE()) { option.appendChild(new DomText(option.getPage(), "")); } } } }
/** * Sets the "selected" state of the specified option. If this "select" element * is single-select, then calling this method will deselect all other options. * * Only options that are actually in the document may be selected. * * @param isSelected true if the option is to become selected * @param optionValue the value of the option that is to change * @return the page that occupies this window after this change is made (may or * may not be the same as the original page) */ public Page setSelectedAttribute(final String optionValue, final boolean isSelected) { try { return setSelectedAttribute(getOptionByValue(optionValue), isSelected); } catch (final ElementNotFoundException e) { if (getPage().getWebClient().getBrowserVersion().isIE()) { for (final HtmlOption o : getSelectedOptions()) { o.setSelected(false); } } return getPage(); } }
final boolean isSelected, final boolean invokeOnFocus) { try { final boolean attributeOnly = hasFeature(JS_SELECT_SET_VALUES_CHECKS_ONLY_VALUE_ATTRIBUTE) && !optionValue.isEmpty(); final HtmlOption selected; if (attributeOnly) { selected = getOptionByValueStrict(optionValue); selected = getOptionByValue(optionValue); return setSelectedAttribute(selected, isSelected, invokeOnFocus, true, false, true); for (final HtmlOption o : getSelectedOptions()) { o.setSelected(false); return (P) getPage();
/** * Sets the value of the {@code selected} property. * @param selected the new selected property */ @JsxSetter public void setSelected(final boolean selected) { final HtmlOption optionNode = (HtmlOption) getDomNodeOrNull(); final HtmlSelect enclosingSelect = optionNode.getEnclosingSelect(); if (!selected && optionNode.isSelected() && enclosingSelect != null && !enclosingSelect.isMultipleSelectEnabled()) { enclosingSelect.getOption(0).setSelectedFromJavaScript(true); } else { optionNode.setSelectedFromJavaScript(selected); } }
/** * Sets the index property. * @param index the index * @param start the scriptable object that was originally invoked for this property * @param newValue the new value */ @Override public void put(final int index, final Scriptable start, final Object newValue) { if (newValue == null) { // Remove the indexed option. htmlSelect_.removeOption(index); } else { final HTMLOptionElement option = (HTMLOptionElement) newValue; final HtmlOption htmlOption = option.getDomNodeOrNull(); if (index >= jsxGet_length()) { // Add a new option at the end. htmlSelect_.appendOption(htmlOption); } else { // Replace the indexed option. htmlSelect_.replaceOption(index, htmlOption); } } if (jsxGet_length() == 1 && !htmlSelect_.isMultipleSelectEnabled()) { ((HTMLSelectElement) htmlSelect_.getScriptObject()).jsxSet_selectedIndex(0); } }
/** * {@inheritDoc} */ public NameValuePair[] getSubmitKeyValuePairs() { final String name = getNameAttribute(); final List<HtmlOption> selectedOptions = getSelectedOptions(); final NameValuePair[] pairs = new NameValuePair[selectedOptions.size()]; int i = 0; for (final HtmlOption option : selectedOptions) { pairs[i++] = new NameValuePair(name, option.getValueAttribute()); } return pairs; }
private String getSelectOptionValueForLabel(HtmlSelect sel, String label) { for (HtmlOption opt : sel.getOptions()) { if (opt.asText().equals(label)) { return opt.getValueAttribute(); } } throw new RuntimeException("Unable to find option " + label + " for " + sel.getNameAttribute()); }
private void doSelectOption(final HtmlOption selectedOption, final boolean isSelected) { // caution the HtmlOption may have been created from js and therefore the select now need // to "know" that it is selected if (isMultipleSelectEnabled()) { selectedOption.setSelectedInternal(isSelected); } else { for (final HtmlOption option : getOptions()) { option.setSelectedInternal(option == selectedOption && isSelected); } } }
/** * Returns the object at the specified index. * * @param index the index * @param start the object that get is being called on * @return the object or NOT_FOUND */ @Override public Object get(final int index, final Scriptable start) { if (htmlSelect_ == null || index < 0) { return Undefined.instance; } if (index >= htmlSelect_.getOptionSize()) { if (getBrowserVersion().hasFeature(JS_SELECT_OPTIONS_NULL_FOR_OUTSIDE)) { return null; } return Undefined.instance; } return getScriptableFor(htmlSelect_.getOption(index)); }
HtmlSelect select = page.getHtmlElementById("myId"); for (HtmlOption o : select.getOptions()) { if (o.getValueAttribute().contains("myValue")) { select.setSelectedAttribute(o, true); } }
private String[] getSelectedOptions(HtmlSelect sel) { String[] result = new String[sel.getSelectedOptions().size()]; int i = 0; for (HtmlOption opt : sel.getSelectedOptions()) { result[i++] = opt.getValueAttribute(); } return result; }
HtmlSelect select = ((HtmlOption) element).getEnclosingSelect(); List<HtmlOption> allOptions = select.getOptions(); for (int i = 0; i < allOptions.size(); i++) { HtmlOption option = select.getOption(i); if (element.equals(option)) { return String.valueOf(i);
/** * Process {@link HtmlSelect}. * * @param builder the StringBuilder to add to * @param htmlSelect the target to process */ protected void appendSelect(final StringBuilder builder, final HtmlSelect htmlSelect) { final List<HtmlOption> options; if (htmlSelect.isMultipleSelectEnabled()) { options = htmlSelect.getOptions(); } else { options = htmlSelect.getSelectedOptions(); } for (final Iterator<HtmlOption> i = options.iterator(); i.hasNext();) { final HtmlOption currentOption = i.next(); appendNode(builder, currentOption); if (i.hasNext()) { builder.append(AS_TEXT_BLOCK_SEPARATOR); } } }
/** * {@inheritDoc} */ @Override public void insertBefore(final DomNode newNode) { super.insertBefore(newNode); if (newNode instanceof HtmlOption) { final HtmlOption option = (HtmlOption) newNode; if (option.isSelected()) { getEnclosingSelect().setSelectedAttribute(option, true); } } }
String multipleAttribute = ((HtmlSelect) element).getMultipleAttribute(); if ("".equals(multipleAttribute)) { return trueOrNull(element.hasAttribute("multiple")); HtmlSelect select = ((HtmlOption) element).getEnclosingSelect(); List<HtmlOption> allOptions = select.getOptions(); for (int i = 0; i < allOptions.size(); i++) { HtmlOption option = select.getOption(i); if (element.equals(option)) { return String.valueOf(i);
/** * Returns the value of the {@code selectedIndex} property. * @return the selectedIndex property */ public int getSelectedIndex() { final List<HtmlOption> selectedOptions = getSelectedOptions(); if (selectedOptions.isEmpty()) { return -1; } final List<HtmlOption> allOptions = getOptions(); return allOptions.indexOf(selectedOptions.get(0)); }
/** * {@inheritDoc} * @see SubmittableElement#setDefaultValue(String) */ @Override public void setDefaultValue(final String defaultValue) { setSelectedAttribute(defaultValue, true); }
/** * Returns the value of this element to what it was at the time the page was loaded. */ public void reset() { for (final HtmlOption option : getOptions()) { option.reset(); } }
/** * Adds the option (and create the associated DOM node if needed) before the specified one * or at the end if the specified one in null. * @param newOptionObject the new option to add * @param beforeOption the option that should be after the option to add */ protected void addBefore(final HTMLOptionElement newOptionObject, final HtmlOption beforeOption) { final HtmlSelect select = getHtmlSelect(); HtmlOption htmlOption = newOptionObject.getDomNodeOrNull(); if (htmlOption == null) { htmlOption = (HtmlOption) HTMLParser.getFactory(HtmlOption.TAG_NAME).createElement( select.getPage(), HtmlOption.TAG_NAME, null); } if (beforeOption == null) { select.appendChild(htmlOption); } else { beforeOption.insertBefore(htmlOption); } }