【问题标题】:How to select option from invisible drop-down element in Selenium WebDriver using Java如何使用 Java 从 Selenium WebDriver 中的不可见下拉元素中选择选项
【发布时间】:2013-12-20 19:40:01
【问题描述】:

请任何人帮助我如何在 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>

【问题讨论】:

  • 请提供小提琴
  • 为什么要让 Selenium 选择人类无法选择的元素?
  • @RossPatterson 完全同意。这是不好的测试。

标签: javascript java jquery selenium selenium-webdriver


【解决方案1】:

完全同意 Ross Patterson 的观点,但如果您仍想尝试上述方案,这可能可行...

((JavascriptExecutor)driver).executeScript("$('select#periodId').click();");

【讨论】:

  • 这假定页面上有 jQuery。并非不可能,但仍有许多页面根本不使用它。
【解决方案2】:
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';");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多