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:
<code class="java">dropdown.selectByVisibleText("Last 52 Weeks");</code>
This selects the option whose visible text matches the specified string.
<code class="java">dropdown.selectByIndex(1);</code>
This selects the option based on its position in the dropdown (starting from 0).
<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!