Select functions

A Select function allows you select or deselect values from a drop-down box or a radio button. It includes a list of Selenium API methods to work with select boxes that contain the <select></select> tags. These functions interact with the UI comboboxes to select options.

Select select = new Select(driver.findElement(By.locatorType("path")));

Some helpful snippets using select functions are given below:

  • The selectByIndex(index) method selects an option using the index value. The following is the syntax for this function:
    select.selectByIndex(index);

    Let's get into the bookstore and select a product from a drop-down list. Here, we select the products at the top of the options list using the selectByTndex() method:

    driver.get("http://www.barnesandnoble.com/"); ...

Get Selenium Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.