skip to content

Core Input Commands

The everyday WebElement commands: clicking, typing, clearing, reading state and attributes, and the support-package wrapper for a real dropdown. Each one quietly assumes more than it says.

on this pageshow

explore

questions

15

In Selenium, which three methods does the Select class offer to choose an option, and what does each match?

level: juniorimportance: must knowfreq 82%

answer

  1. A wrapper around a real dropdown
  2. Three different things an option carries
  3. Rendered text, value attribute, position
  4. Position counts from zero
  5. A miss always raises, never returns quietly

basics

~10 s

Selenium's Select class offers selectByVisibleText, which matches an option's rendered text; selectByValue, which matches its value attribute exactly; and selectByIndex, which matches its zero-based position. Each throws NoSuchElementException when nothing matches.

solid answer

~30 s

`Select` wraps a real `<select>` element and gives three ways to name an option. `selectByVisibleText("Resident annual")` matches the option's rendered text with a whitespace-normalising comparison, then retries with a trimmed contains search, so stray markup whitespace does not break it. `selectByValue("resident-annual")` matches the `value` content attribute exactly and case-sensitively. `selectByIndex(2)` matches the option's zero-based `index` property, its position in document order across `<optgroup>` boundaries. All three raise `NoSuchElementException` when nothing matches, and all three refuse a disabled `<select>` or a disabled `<option>` with `UnsupportedOperationException`. Selenium 4 also exposes `selectByContainsVisibleText`, an explicit substring match on the option text.

code

java · 24 lines
java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class PermitRenewalSelect {
  public static void main(String[] args) {
    WebDriver driver = new ChromeDriver();
    try {
      driver.get("https://permits.example.gov/renew");
      WebElement dropdown = driver.findElement(By.id("permit-type"));
      Select permitType = new Select(dropdown);

      permitType.selectByVisibleText("Resident annual");
      permitType.selectByValue("resident-annual");
      permitType.selectByIndex(2);

      System.out.println(permitType.getFirstSelectedOption().getText());
    } finally {
      driver.quit();
    }
  }
}

go deeper

for a junior

Be ready to name the three methods and say what each one matches: rendered text, the value attribute, and zero-based position. Knowing that a miss raises rather than returns quietly is part of the expected answer.

for a middle

Explain the mechanics: the text match normalises whitespace and has a trimmed fallback, the value match is exact and case-sensitive, and the index match reads the option's own index property rather than counting in your code.

for a senior

Show judgment about which method a suite should reach for, and be able to diagnose a failure from its exception alone: a NoSuchElementException naming a value is a very different fault from an UnsupportedOperationException about a disabled select.

for a principal

Own the tradeoff between coupling to backend value codes and coupling to user-visible copy, and be able to say what each choice costs a large suite when either the codes or the wording changes.

## What the Select class is The `Select` class lives in `org.openqa.selenium.support.ui` and ships in the `selenium-support` artifact, not in Selenium's core API. It is a **thin wrapper**, not a driver command: you find the `<select>` element yourself, hand the resulting `WebElement` to the constructor, and the wrapper translates a high-level intention such as "choose Resident annual" into ordinary operations against the `<option>` children. It implements `ISelect` and `WrapsElement`, so `getWrappedElement()` hands the original `<select>` back whenever you need the element itself. On a parking-permit renewal form the permit-type control is a plain browser dropdown: ```html <select id="permit-type" name="permitType"> <option value="resident-annual">Resident annual</option> <option value="resident-quarterly">Resident quarterly</option> <option value="visitor-day">Visitor day pass</option> </select> ``` Every `<option>` carries three independently addressable things: the text the applicant reads, the `value` the form posts, and the position it occupies in the list. `Select` gives you one method per thing. ## The three methods side by side | Method | Matches on | Behaviour on a miss | |---|---|---| | `selectByVisibleText(String)` | the option's rendered text, whitespace-normalised | `NoSuchElementException` | | `selectByValue(String)` | the `value` content attribute, compared exactly | `NoSuchElementException` | | `selectByIndex(int)` | the option's zero-based `index` property | `NoSuchElementException` | Three things are worth noticing about that table: - All three methods change the page in exactly the same way underneath; only the matching rule differs, so their failure modes and their events are identical. - All three report a miss with the same exception type, which means the message text, not the type, is what tells you which criterion failed. - None of them is a search you can narrow further. The wrapper always looks inside the `<select>` you handed it, so a match is scoped to that one control by construction. ## selectByVisibleText, matching what the applicant reads `selectByVisibleText("Visitor day pass")` first searches the wrapped element's option descendants with an XPath that compares `normalize-space()` of the option's text against your string, so indentation and collapsed inner runs of whitespace in the markup do not defeat it. If that exact comparison finds nothing, the method falls back: it takes the longest space-free token of your string, looks for options **containing** that token, and then accepts only an option whose trimmed text equals your trimmed string. The fallback recovers from stray leading and trailing whitespace without ever quietly landing on a different option. A matching option that is hidden by `visibility: hidden`, `display: none` or an opacity of zero is refused with `NoSuchElementException("Invisible option with text: ...")` instead of being clicked. Selenium 4 also exposes `selectByContainsVisibleText(String)`, a deliberate substring match — and that one genuinely can select an option you did not mean. ## selectByValue, matching what the form posts `selectByValue("visitor-day")` matches the `value` **content attribute** exactly and case-sensitively. There is no whitespace normalisation and no substring fallback: the value either matches or you get `NoSuchElementException("Cannot locate option with value: ...")`. It is the method that survives copy changes, because the renewal form's label may become "Visitor (1 day)" between releases while `visitor-day` stays part of the request the server parses. ## selectByIndex, matching position `selectByIndex(2)` does not count list entries in your test code. It reads each option's `index` property and compares it with the number you passed, which is why the Javadoc describes the choice as made by examining the index "and not merely by counting". The property is **zero-based** and numbers every option of the select in document order, including options nested inside `<optgroup>` groupings, so `selectByIndex(2)` on the markup above chooses "Visitor day pass". ## Choosing between the three 1. **Prefer `selectByValue`** when the permit codes are a contract the backend owns; they move far less often than the label an editor can rewrite. 2. **Use `selectByVisibleText`** when the point of the step really is the applicant's own words and you want a wrong label to be visible. 3. **Reach for `selectByIndex` last.** A zone list ordered by a database query renumbers itself the moment a zone is added, and index 2 silently becomes a different zone. ## What all three refuse to do - A **disabled `<select>`** raises `UnsupportedOperationException("You may not select an option in disabled select")` before any matching is attempted. - A **disabled `<option>`** raises `UnsupportedOperationException("You may not select a disabled option")`, so an unavailable permit tier fails loudly rather than appearing chosen. - `selectByVisibleText` additionally refuses an **invisible `<select>`** with `UnsupportedOperationException("You may not select an option in invisible select")`. - **No match at all** is always `NoSuchElementException`, and its message names the text, value or index that could not be located. None of this involves polling. `Select` issues its commands immediately, so if the renewal form fills its permit-type options from a background request, the options have to be in the DOM by the time you construct the wrapper and call a method, or the call fails at once.

  • Which of the three is least likely to break when the page's wording changes, and why?
    `selectByValue`. The `value` attribute is part of the request the server parses, so it is a contract the backend owns and changes rarely. Visible text is editorial copy that a content change can rewrite, and index depends on list ordering that a new permit tier renumbers. Value costs nothing extra to use and fails loudly with `NoSuchElementException` if the contract really did change.
  • What does selectByVisibleText do if the option's text has extra whitespace in the markup?
    It still matches. The first attempt compares the whitespace-normalised text of each option, so indentation and collapsed inner runs are absorbed. If that misses, it retries by searching for options containing the longest space-free token of your string and accepting one whose trimmed text equals your trimmed string. Only if both attempts fail does it raise `NoSuchElementException`.
  • Does Select wait for the options to appear before matching?
    No. `Select` sends its commands immediately and does not poll. If a permit-type dropdown fills its options from a background request, the options must already be in the DOM when you construct the wrapper and call a method; otherwise the call fails at once with `NoSuchElementException`. Any waiting has to happen before the wrapper is used.

saying these in an interview costs you the question

  • Thinks selectByIndex counts from one rather than zero
  • Believes selectByValue matches the option's visible label
  • Assumes a missing option leaves the dropdown silently unchanged
  • Thinks selectByVisibleText does a substring match by default
  • Expects Select to wait for options that have not rendered yet
open as a page

Why does Selenium's Select class throw UnexpectedTagNameException on a dropdown built from div elements?

level: middleimportance: must knowfreq 68%

basics

~20 s

Selenium's Select constructor reads the element's tag name and accepts only a select tag. A styled listbox made of div and span elements has no option children and no multiple attribute, so the wrapper rejects it at construction.

open as a page

In Selenium 4, how do WebElement.getDomAttribute and getDomProperty differ, and where does getAttribute fit?

level: middleimportance: must knowfreq 76%

basics

~20 s

getDomAttribute reads the value the page's HTML declared. getDomProperty reads the element's live DOM property, so it sees what the user typed or ticked. getAttribute is the older combined call: it prefers the property and falls back to the attribute.

open as a page

What changes in Selenium's Select class when the wrapped select element carries the multiple attribute?

level: seniorimportance: must knowfreq 58%

basics

~20 s

Selecting by text or value stops selecting every match instead of just the first, the whole deselect family becomes legal, and isMultiple reports true. Selenium's Select reads that attribute once, in the constructor, and caches the answer.

open as a page

In Selenium, what does WebElement.isDisplayed() actually compute, and why is it not normatively specified?

level: seniorimportance: must knowfreq 61%

basics

~20 s

It runs a bundled Selenium script that approximates visibility by walking the element tree: display none, visibility hidden, zero opacity, zero size and unscrollable overflow all make it false. The W3C specification only recommends this approach in an appendix.

open as a page

In Selenium, how do you choose between Keys.ENTER, clicking the submit button, and WebElement.submit()?

level: principalimportance: must knowfreq 46%

basics

~20 s

Click the real control by default, press Enter when the case covers the keyboard journey, and avoid submit. In Selenium 4 submit runs an injected script, so it skips every scroll and interactability check the click command performs.

open as a page

How would you move a large Selenium 4 suite off getAttribute onto getDomAttribute and getDomProperty?

level: principalimportance: must knowfreq 52%

basics

~20 s

Treat it as a semantic audit, not a rename. Group call sites by the name being read, move markup-only names first, turn boolean flags into isSelected and isEnabled, change live-value reads last, then ban the old call.

open as a page

In Selenium, what scrolling does WebElement.click() perform before clicking, and when does it skip it?

level: middleimportance: should knowfreq 52%

basics

~20 s

It calls scrollIntoView with instant behaviour, block end and inline nearest, so the bottom of the element is aligned with the bottom of the viewport. It skips the scroll when the element is already in view.

open as a page

In Selenium, why does WebElement.sendKeys append rather than replace an input's existing value?

level: middleimportance: should knowfreq 66%

basics

~20 s

Send Keys never resets the field. When the input is not already focused the driver focuses it and puts the caret after the existing text, then types character by character, so the new characters land on the end.

open as a page

In Selenium, how does the Select class actually change which option of a dropdown is selected?

level: middleimportance: should knowfreq 47%

basics

~20 s

It clicks the option element. Selenium's Select checks the option is enabled, compares its current selectedness with the state you asked for, and clicks only when they differ, so re-selecting an already-chosen option sends nothing at all.

open as a page

In Selenium, why can WebElement.getText() return an empty string for an element whose markup has text?

level: middleimportance: should knowfreq 67%

basics

~20 s

Because it returns rendered text, not markup text. Anything the browser did not draw contributes nothing, so an element hidden by display none or visibility hidden yields an empty string even though its characters are in the page.

open as a page

In Selenium, why can clear() empty a framework-controlled input yet leave the app using the old text?

level: seniorimportance: should knowfreq 47%

basics

~20 s

Clear resets the value through the HTML clear algorithm and dispatches no key events at all. A component that keeps its own copy of the field's value and updates it from typing can miss the reset, so its copy stays stale.

open as a page

In Selenium, where on an element does WebElement.click() actually land, and how is that point computed?

level: seniorimportance: should knowfreq 41%

basics

~10 s

Selenium clicks the in-view centre point: the centre of the intersection between the element's first client rectangle and the viewport, floored to whole pixels. Whatever is painted at that one coordinate receives the click.

open as a page

In Selenium, what do Select's getOptions and getAllSelectedOptions return, and what does each call cost?

level: seniorimportance: should knowfreq 44%

basics

~20 s

getOptions runs a fresh search for the select's option elements and returns a snapshot list, not a live collection. getAllSelectedOptions filters that list by asking each option whether it is selected, so it costs one find plus one command per option.

open as a page

What does Selenium's WebElement.getAriaRole() return, and how does it differ from reading the role attribute?

level: seniorimportance: nice to knowfreq 21%

basics

~20 s

It returns the WAI-ARIA role the browser computed for the element, including the implicit role of the tag. Reading the role attribute returns only the literal token the markup declared, or nothing when the markup declared none.

open as a page