在我的应用程序中,我有两个 <select>
标签。第一个更改第二个选项内的选项并在 onchange 事件中启用它。
当我使用 Selenium2 提供的 Select 对象时,它在 IE8 中运行时不会触发该事件(在 FF 中工作得很好,当我手动执行时)。
Select select = new Select(getElementByName(name));
element.selectByValue(value);
第一个<select>
变化如预期。然而,第二个<select>
保持为空且禁用。我尝试将此作为解决方法:
if(ie) {
WebElement select = getElementByName(name);
WebElement option = select.findElement(By.cssSelector("[value='"+value+"']"));
List<WebElement> options = select.findElements(By.cssSelector("option"));
//select the first element
options.get(0).click();
//make sure the select is focused
select.click(); //open
select.click(); //close
Keyboard keyboard = getWebDriver().getKeyboard();
for(int i = 0; i < options.size() && option.getAttribute("selected") == null; i++) {
keyboard.pressKey(Keys.DOWN);
//note: if i do a Thread.sleep(100); here, it works more consistently, but still not 100%
}
} else {
// Do the above snippet
}
但现在我得到的结果不一致。所需的<option>
总是会被选中,而只有有时该事件才会被触发。
显然,最好的选择是让 Select 在 IE8 中工作。还有其他人看到这个问题吗?看起来像是 Selenium2 中的一个错误。是否有已知的解决方法?
最佳答案
在 #selenium IRC 聊天室与一些 Selenium 人员交谈后,我决定进行此修复:
WebElement selectElement = getElementByName(name);
Select select = new Select(selectElement);
element.selectByValue(value);
if(usingIE) {
webDriver.executeScript("$(arguments[0]).fireEvent('change');", selectElement);
}
关于java - Selenium2 的 Select 对象不会在 IE8 中触发 onchange 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8346761/