获取Selenium Webdriver在下拉菜单中选择的值

时间:2014-02-28 10:01:43

标签: java selenium selenium-webdriver

我有一个预先选择了值的下拉菜单请帮我如何获取所选的值文本并在控制台中打印

下拉菜单html代码

<select id="ctl00_ContentPlaceHolder1_ddlCalculation" class="normalText" 
    style="width:100%;"
    onchange="javascript:LoadMethods(this.value);
              CallonChange(this.value,'spn_ddlCalculation');
              return false;"
    disabled="disabled" name="ctl00$ContentPlaceHolder1$ddlCalculation">

    <option value="0" selected="selected">--- Select ---</option>
    <option value="f">Formula Based</option>
    <option value="m">Formula Based with Matrix Table</option>
    <option value="q">Quantity Based</option>
    <option value="t">Time Based</option>
</select>

1 个答案:

答案 0 :(得分:0)

请尝试以下操作并告诉我它是否有效:

Select dropdown = new Select(driver.findElement("Use the correct selector");
WebElement option = dropdown.getFirstSelectedOption();
String content = option.getText();
System.out.println("selected Value " + content);