我在使用Selenium Webdriver进行测试时遇到问题。我正在使用Java。无法从非“选择”类别的多个下拉列表中选择。下拉菜单如下所示:
这就是代码:
<div class="form-group ">
<label for="CurrentCategoriesNomIds-selectized">Categories</label>
<select placeholder="" multiple="multiple" id="CurrentCategoriesNomIds" name="CurrentCategoriesNomIds" tabindex="-1" class="selectized" style="display: none;">
<option value="325" selected="selected">Education</option>
</select>
<div class="selectize-control multi plugin-remove_button">
<div class="selectize-input items not-full has-options has-items">
<div class="item" data-value="325">
Education
<a href="javascript:void(0)" class="remove" tabindex="-1" title="Remove">×</a>
</div>
<input type="text" autocomplete="off" tabindex="" id="CurrentCategoriesNomIds-selectized" style="width: 4px; opacity: 1; position: relative; left: 0px;"></div>
<div class="selectize-dropdown multi plugin-remove_button" style="display: none; visibility: visible; width: 800px; top: 36px; left: 0px;">
<div class="selectize-dropdown-content">
<div class="option" data-selectable="" data-value="324">Agriculture</div>
<div class="option" data-selectable="" data-value="298">Culture</div>
<div class="option" data-selectable="" data-value="326">Employment</div>
<div class="option" data-selectable="" data-value="323">Environment</div>
<div class="option" data-selectable="" data-value="327">Other</div>
<div class="option" data-selectable="" data-value="297">Political</div>
<div class="option" data-selectable="" data-value="322">Transport</div>
</div>
</div>
</div>
</div>
这是当选择2个选项时的外观。我想知道是否可以尝试使用KEYS,但页面无法正常运行。以前没有见过这种领域,并且不确定如何进行吗?
答案 0 :(得分:1)
您可以使用以下代码单击下拉列表:
public static void selectOption(WebDriver driver, String optionName) {
List<WebElement> options = driver.findElements(By.xpath("//div[@class='selectize-dropdoun-content']//div[@class='option']"));
options.forEach(option -> {
if (option.getAttribute("innerText").equals(optionName)) {
Actions actions = new Actions(driver);
actions.moveToElement(option).click().build().perform();
}
});
}
然后像这样使用:
String option = "Education";
selectOption(driver,option);
希望对您有帮助:)
添加我在网站https://semantic-ui.com/modules/dropdown.html上尝试过的屏幕截图
答案 1 :(得分:0)
我不太使用Java,因此我将为此编写一些伪代码,这些伪代码应该为您概述如何实现它(但可能不会以书面形式运行)。
public static void selectOptionFromSelectizeDropdown(String optionText, String dropdown){
boolean completed = false;
int numberOfOptions = driver.findElements(By.css(dropdown + " .option")).length
for(int i = 0; i < numberOfOptions && completed === false; i++){
// Check if it's displayed, if it is, HUZZAH! Click the option
if(driver.findElement(By.xpath('//*/*[contains(@class, "option") and contains(text(), "'+optionText+'")])')).isDisplayed()){
driver.findElement(By.xpath('//*/*[contains(@class, "option") and contains(text(), "'+optionText+'")])')).click();
completed === true;
break;
} else {
// In case there are many options, and you have to scroll through them.
int x = 0;
while(x <= 6){
driver.findElement(By.css(dropdown)).sendKeys(Keys.DOWN);
i++;
}
}
if(i===numberOfOptions - 1){
throw new Error("Option Not Found");
}
}
}
selectOptionFromSelectizeDropdown("Education", ".selectize-dropdown-content");
如果这不起作用,建议您将click()
更改为sendKeys(Key.ENTER)
,以查看是否可行。
说明
将遍历,查看该选项是否显示在页面上。如果没有,将向下滚动x次,然后再次检查,直到找到该选项。
如果达到框内选项的数量,则会引发错误。