Home  >  Article  >  Java  >  How to Select Dropdown Values with Selenium WebDriver in Java?

How to Select Dropdown Values with Selenium WebDriver in Java?

Linda Hamilton
Linda HamiltonOriginal
2024-10-25 03:54:29165browse

How to Select Dropdown Values with Selenium WebDriver in Java?

Selecting Dropdown Values with Selenium WebDriver in Java

When automating web applications with Selenium WebDriver, selecting values from dropdowns is a common requirement. In Java, this can be achieved using the Select class, which wraps the WebElement representing the dropdown.

To work with the dropdown identified by the HTML tag with id="periodId":

<code class="java">Select dropdown = new Select(driver.findElement(By.id("periodId")));</code>

Once you have a Select object, you can select values in various ways:

  1. By Visible Text:
<code class="java">dropdown.selectByVisibleText("Last 52 Weeks");</code>

This selects the option whose visible text matches the specified string.

  1. By Index:
<code class="java">dropdown.selectByIndex(1);</code>

This selects the option based on its position in the dropdown (starting from 0).

  1. By Value Attribute:
<code class="java">dropdown.selectByValue("l52w");</code>

This selects the option whose value attribute matches the specified value.

The above is the detailed content of How to Select Dropdown Values with Selenium WebDriver in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn