如何使用Java从SeleniumWebDriver中的不可见下拉元素中选择选项

How to select option from invisible drop-down element in Selenium WebDriver using Java

本文关键字:元素 选项 选择 Java 何使用 SeleniumWebDriver      更新时间:2024-02-21

请任何人帮助我如何在Selenium WebDriver中找到不可见元素。我想从下拉列表中选择一个选项,我的目标是通过ID找到元素。

但在HTML标记中,元素不可见,无法选择选项。我验证了很多问题,他们提到使用JavascriptExceutor。

有人能帮我写html标签的java脚本吗:

<select id="periodId" name="period" style="display: none;">
<option value="l4w">Last 4 Weeks</option>
<option value="l52w">Last 52 Weeks</option>
<option value="daterange">Date Range</option>
<option value="weekrange">Week Range</option>
<option selected="" value="monthrange">Month Range</option>
<option value="yeartodate">Year To Date</option>
</select>

完全同意Ross Patterson的观点,但如果你仍然想尝试上述场景,这可能会奏效。。。

((JavascriptExecutor)driver).executeScript("$('select#periodId').click();");
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('periodId').style.display='inline';");
Select select = new Select(driver.findElement(By.id("periodId")));
select.selectByVisibleText("Last 4 Weeks");
executor.executeScript("document.getElementById('periodId').style.display='none';");