简体   繁体   English

Selenium2的Select对象未触发IE8中的onchange事件

[英]Selenium2's Select object not firing onchange event in IE8

In my app I have two <select> tags. 在我的应用程序中,我有两个<select>标签。 The first one changes the options inside the second one and enables it in the onchange event. 第一个在第二个内部更改选项,并在onchange事件中启用它。

When I use the Select object provided by Selenium2 it doesn't fire that event when running in IE8 (works great in FF and when I do it manually). 当我使用Selenium2提供的Select对象时,在IE8中运行时不会触发该事件(在FF中以及手动执行时效果很好)。

Select select = new Select(getElementByName(name));
element.selectByValue(value);

The first <select> changes as expected. 第一个<select>会按预期更改。 However, the second <select> remains empty and disabled. 但是,第二个<select>保持为空并被禁用。 I tried this as a workaround: 我尝试了这种解决方法:

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
}

but now I get inconsistent results. 但是现在我得到不一致的结果。 The desired <option> always gets selected, while only sometimes does the event get fired. 总是选择所需的<option> ,而仅在某些情况下会触发该事件。

Obviously the best option is getting the Select to work in IE8. 显然,最好的选择是使Select在IE8中工作。 Has anyone else seen this issue? 还有其他人看到过这个问题吗? Seems like a bug in Selenium2. 似乎是Selenium2中的错误。 Is there a known workaround for this? 是否有已知的解决方法?

After talking with some of the Selenium folk in the #selenium IRC chat room I settled on this fix: 在#selenium IRC聊天室中与一些Selenium成员交谈之后,我决定解决此问题:

WebElement selectElement = getElementByName(name);
Select select = new Select(selectElement);
element.selectByValue(value);
if(usingIE) {
    webDriver.executeScript("$(arguments[0]).fireEvent('change');", selectElement);
}

It looks like you are already implementing the SelectElement class so have you tried this 看来您已经在实现SelectElement类,所以您尝试了吗

WebElement element = getElementByName(name);
element.FindElement(By.CssSelector("option[value='" + value + "']").Select();

Am using below code to select a value in 'Country' list (once 'Country' value is selected, corresponding 'State' list is loading): 我正在使用下面的代码在“国家/地区”列表中选择一个值(一旦选择“国家/地区”值,则正在加载相应的“国家”列表):

WebElement selectCountry = driver.findElement(By.id("country"));
List<WebElement> options = selectCountry.findElements(By.tagName("option"));
for(WebElement option : options){
    if(option.getText().equalsIgnoreCase("India")){
        option.click();
        break;
    }
}

Note - This select operation takes much more time IE when compare to FF. 注意-与FF相比,此选择操作需要更多的时间IE。 You may need to increase command timeout time using driver.manage(). 您可能需要使用driver.manage()增加命令超时时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM