尝试使用Selenium获取C#中下拉菜单的当前值

时间:2018-08-22 14:46:49

标签: c# visual-studio selenium selenium-chromedriver

我具有以下代码设置,该代码设置可以按预期通过报告的网页选项使用。现在,我想在单击“提交”后提取每个Web元素的值,以便随后将其用于发送到SQL查询。我目前一直无法从网页的当前状态中获取值。

如您所见,我正在按索引号从下拉列表中拉出。请客气,我只使用C#和硒一个月左右.....

Open_TM_DD_URT();
        WAIT();
        var EntryType_URT = Driver.FindElement(By.Id("entryTypeDropdown"));
        var selectElement_URT = new SelectElement(EntryType_URT);
        WAIT();
        selectElement_URT.SelectByIndex(2);
        WAIT();

        var FiscalYear = Driver.FindElement(By.Id("fiscalYearsDropdown"));
        var Fiscal_Select = new SelectElement(FiscalYear);

        var BEC = Driver.FindElement(By.Id("businessEventCodesDropdown"));
        var BEC_Code = new SelectElement(BEC);

        SLEEP();
        for (int i = 1; i <= 15; i++)
        {
            Fiscal_Select.SelectByIndex(i);
            WAIT();
            SLEEP();
            for (int b = 1; b <= 53; b++)
            {
                BEC_Code.SelectByIndex(b);
                WAIT();
                SLEEP();
                HIT_SUBMIT_BTN();
                SLEEP();

                //this is where i need to get value of the two drop menus

这是我要提取的HTML。

    <select _ngcontent-c8="" class="form-control ng-valid ng-touched ng-dirty" id="fiscalYearsDropdown" name="fiscalYearsDropdown" ng-reflect-name="fiscalYearsDropdown" ng-reflect-is-disabled="false" ng-reflect-model="2019">
                    <!--bindings={
      "ng-reflect-ng-if": "false"
    }-->
                    <!--bindings={
      "ng-reflect-ng-if": "true"
    }--><option _ngcontent-c8="" value="-1" ng-reflect-value="-1" class="ng-star-inserted">Select Fiscal Year</option>
                    <!--bindings={
      "ng-reflect-ng-for-of": "2019,2018,2017,2016,2015,2014,"
    }--><option _ngcontent-c8="" value="2019" ng-reflect-value="2019" class="ng-star-inserted">2019</option>
<option _ngcontent-c8="" value="2018" ng-reflect-value="2018" class="ng-star-inserted">2018</option>
<option _ngcontent-c8="" value="2017" ng-reflect-value="2017" class="ng-star-inserted">2017</option>
<option _ngcontent-c8="" value="2016" ng-reflect-value="2016" class="ng-star-inserted">2016</option>
<option _ngcontent-c8="" value="2015" ng-reflect-value="2015" class="ng-star-inserted">2015</option>
<option _ngcontent-c8="" value="2014" ng-reflect-value="2014" class="ng-star-inserted">2014</option>
<option _ngcontent-c8="" value="2013" ng-reflect-value="2013" class="ng-star-inserted">2013</option>
<option _ngcontent-c8="" value="2012" ng-reflect-value="2012" class="ng-star-inserted">2012</option>
<option _ngcontent-c8="" value="2011" ng-reflect-value="2011" class="ng-star-inserted">2011</option>
<option _ngcontent-c8="" value="2010" ng-reflect-value="2010" class="ng-star-inserted">2010</option>
<option _ngcontent-c8="" value="2009" ng-reflect-value="2009" class="ng-star-inserted">2009</option>

1 个答案:

答案 0 :(得分:0)

该选项具有属性“值”和可见文本,在您的示例中这是相等的,但可以不同。您可以使用下面的任何代码。

要获取所选选项的文本:

var fiscalSectedText = Fiscal_Select.SelectedOption.Text;

要获取所选选项的值属性:

var fiscalSelectedValue = Fiscal_Select.SelectedOption.GetAttribute("value")