Jobs Platform
Selenium with Java and TestNG: Level 1

Lesson 3 of 8 · 9 min

Working with elements and dropdowns

Once you have a WebElement, you can act on it and read from it. These few methods cover most of what a test does on a page. The examples use the same small login form as the last lesson: a username input, a password input and a Login button.

Actions

WebElement user = driver.findElement(By.id("username"));
user.sendKeys("Asha");
driver.findElement(By.name("pass")).sendKeys("secret");
driver.findElement(By.id("login")).click();
  • click() clicks the element.
  • sendKeys(text) types into an input. It adds to whatever is already there.
  • clear() removes the text of an input.
  • submit() is rarely needed; clicking the real submit button is closer to what a user does.

Because sendKeys adds text, typing twice gives joined text. The next code was run to show it:

user.sendKeys("abc");
user.sendKeys("xyz");
System.out.println(user.getDomProperty("value"));
user.clear();
user.sendKeys("xyz");
System.out.println(user.getDomProperty("value"));
abcxyz
xyz

Reading from elements

  • getText() returns the visible text, for example the text of a heading, paragraph or button.
  • getDomProperty("value") returns what is typed in an input. getText() on an input returns an empty string, because the typed text is a value, not page text.
  • getAttribute("name") reads an attribute. In Selenium 4, getDomAttribute reads the attribute exactly as written in the HTML, while getAttribute may give a processed value. For a link <a href="/jobs">, getDomAttribute("href") gives /jobs and getAttribute("href") gives the full web address.
  • getTagName() returns the tag, such as input.

Checking state

  • isDisplayed() is true when a person can see the element.
  • isEnabled() is false for disabled controls.
  • isSelected() tells if a checkbox, radio button or option is selected.
WebElement heading = driver.findElement(By.id("heading"));
System.out.println(heading.getText());
System.out.println(heading.isDisplayed());
Practice Page
true

These checks are the base of test assertions: find the element, read the value, and compare it with what you expect.

Dropdowns with Select

For a normal HTML <select> element, wrap it in the Select class from org.openqa.selenium.support.ui. The page used below has three options: Chennai (value chn), Bengaluru (value blr) and Hyderabad (value hyd).

Select city = new Select(driver.findElement(By.id("city")));
city.selectByVisibleText("Hyderabad");
System.out.println(city.getFirstSelectedOption().getText());
city.selectByValue("blr");
System.out.println(city.getFirstSelectedOption().getText());
city.selectByIndex(0);
System.out.println(city.getFirstSelectedOption().getText());
System.out.println(city.getOptions().size());
Hyderabad
Bengaluru
Chennai
3

Index numbers start at 0. If you pass an element that is not a select, such as an input, the constructor throws UnexpectedTagNameException. Many modern sites build dropdowns from div and li tags. Select does not work with those. Click the dropdown, wait for the list, and click the option like any other element.

Try it yourself

  • Create a small HTML file with a select list, and select each option by text, value and index.
  • Type into a field, then print its value with getDomProperty and with getText. Note the difference.
  • Find a disabled button on any page and print isEnabled().
  • Write a helper method fillLogin(String user, String pass) that types in both fields.

Quick check

Three quick questions on this lesson. Your answers are not saved.

  1. Question 1 of 3

    An input already contains hello. You call sendKeys("hi"). What does the input contain now?

  2. Question 2 of 3

    Which method tells you whether a checkbox is ticked?

  3. Question 3 of 3

    A dropdown is built from div and li tags, not a select tag. What is the right approach?