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

How to Select Dropdown Values with Selenium WebDriver in Java?

Linda Hamilton
Release: 2024-10-25 03:54:29
Original
297 people have browsed it

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>
Copy after login

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>
Copy after login

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

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

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template