我正在努力使用Edge WebDriver自动删除下拉列表
驱动程序版本10.0.1586.0
public void selectFromDropdown(By dropdownLocator, String optionToChoose) {
Select select = new Select(getElementWhenPresent(dropdownLocator));
select.selectByVisibleText(optionToChoose);
}
无法使用Edge WebDriver ,但适用于IE8-11,Chrome,FF 。实际上下拉列表会在测试运行期间更改其值,但Edge浏览器会忽略它。浏览器/驱动程序错误?
修改的
使用jQuery设置下拉值也不会触发更改事件
public void selectFromDropdown(By dropdownLocator, String optionToChoose) {
if(this.getName() == BrowserName.EDGE || this.getName() == BrowserName.EDGE_GRID){
// Select option with jQuery
JavascriptExecutor executor = (JavascriptExecutor) driver;
initializeJQuery();
String $dropdown = "#" + getElementWhenPresent(dropdownLocator).getAttribute("id");
executor.executeScript("$(\"" + $dropdown + "\").val($(\"" + $dropdown + " > option\").filter(function () { return $(this).html() == \"TPI\"; }).val())");
} else {
Select select = new Select(getElementWhenPresent(dropdownLocator));
select.selectByVisibleText(optionToChoose);
}
}
执行的jQuery代码:
$("#selectId").val($("#selectId> option").filter(function () { return $(this).html() == "OPTION TEXT"; }).val())
修改的
下拉元素的HTML(这里没什么特别的):
<select class="form-control" id="operations">
<option value="-1_*">Name 1</option>
<option value="9_*">Name 2</option>
<option value="16_B7">Name 3</option>
</select>
答案 0 :(得分:2)
最后使用sendKeys()解决了这个问题,所以我正在检查是否在Edge上执行测试并使用sendKeys()方法设置<select>
选项:
public void selectFromDropdown(By dropdownLocator, String optionToChoose) {
if(this.getName() == BrowserName.EDGE){
getElementWhenPresent(dropdownLocator).sendKeys(optionToChoose);
} else {
Select select = new Select(getElementWhenPresent(dropdownLocator));
select.selectByVisibleText(optionToChoose);
}
}