我有以下代码设置,它可以按预期工作,以浏览报告的网页选项。 我现在想在单击提交后提取每个 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>
该选项具有属性"value"和可见文本,在您的示例中它们是相等的,但可以不同。您可以使用以下任何代码。
获取所选选项的文本:
var fiscalSectedText = Fiscal_Select.SelectedOption.Text;
获取所选选项的值属性:
var fiscalSelectedValue = Fiscal_Select.SelectedOption.GetAttribute("value")