我是自动化的新手,目前正在为我的应用程序探索Selenium Java 尝试使用Selenium Java自动化Web应用程序。
我在网上看过,只有下拉菜单为“选择”时才能找到答案。请建议我如何从下拉列表中选择一个值。
HTML代码:
<span title="" class="k-widget k-dropdown k-header innova-invalid" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="" aria-disabled="false" aria-busy="false" aria-activedescendant="32406016-a12b-4ce6-a9f7-56f84a0883bd" style="" xpath="1">
<span unselectable="on" class="k-dropdown-wrap k-state-default" style="">
<span unselectable="on" class="k-input"></span>
<span unselectable="on" class="k-select" aria-label="select"></span>
</span>
<select kendo-dropdownlist="$ctrl.dropdownList" k-data-source="$ctrl.dataSourceOptions.dataSource" k-data-text-field="'Display'" k-data-value-field="$ctrl.valueField" k-value-primitive="$ctrl.valuePrimitive" name="HSRisk" k-options="$ctrl.options" k-ng-model="$ctrl.model" k-rebind="$ctrl.rebindTimestamp" k-ng-disabled="$ctrl.isDisabled" ng-class="{'innova-invalid': $ctrl.hasError}" data-role="dropdownlist" style="display: none;" class="innova-invalid">
<option value="true"></option>
<option value="false"></option>
</select>
</span>
<span unselectable="on" class="k-dropdown-wrap k-state-default" style="">
<span unselectable="on" class="k-input"></span>
<span unselectable="on" class="k-select" aria-label="select">
<span class="k-icon k-i-arrow-60-down"></span>
</span>
</span>
<select kendo-dropdownlist="$ctrl.dropdownList" k-data-source="$ctrl.dataSourceOptions.dataSource" k-data-text-field="'Display'" k-data-value-field="$ctrl.valueField" k-value-primitive="$ctrl.valuePrimitive" name="HSRisk" k-options="$ctrl.options" k-ng-model="$ctrl.model" k-rebind="$ctrl.rebindTimestamp" k-ng-disabled="$ctrl.isDisabled" ng-class="{'innova-invalid': $ctrl.hasError}" data-role="dropdownlist" style="display: none;" class="innova-invalid">
<option value="true">
Yes
</option>
<option value="false">
No
</option>
</select>
</span>
答案 0 :(得分:1)
您不能在选择下拉菜单中单击选项。您需要以不同的方式处理它们。
从您的代码示例中,我想说,要选择一个选项,您必须这样做:
代码示例
Select yourSelectSection = new Select(driver.findElement(By.id("YOUR_ID")));
yourSelectSection.selectByIndex(0); // by using index
yourSelectSection.selectByVisibleText("Yes"); // or by using text
注意:您必须导入 Select 库。如果您需要了解有关此库的更多信息,可以进行here。
答案 1 :(得分:0)
Select yourSelectSection = new Select(driver.findElement(By.Xpath("dropdown-list")));
yourSelectSection.selectByIndex(0); // by using index
yourSelectSection.selectByVisibleText("true");
这会起作用
答案 2 :(得分:0)
请尝试以下代码:
driver.findElement(By.id(“dropdownField”)).sendKeys(“mention text of required value from dropdown”); //send required option in dropdown field
driver.findElement(By.id(“option1”)).click();
或尝试使用以下代码:
List<WebElement> options = driver.findElements(By.xpath(“”));
for(WebElement option : options) {
if (option.getText().contains(“mention text of required value from dropdown”)) {
option.click();
break;
}
请参阅 link 了解更多详细信息 - 如何在不使用 Selenium 中 Select 类的方法的情况下在下拉列表中选择特定值。