Java 硒2';s在IE8中选择不触发onchange事件的对象

Java 硒2';s在IE8中选择不触发onchange事件的对象,java,internet-explorer-8,webdriver,selenium-webdriver,Java,Internet Explorer 8,Webdriver,Selenium Webdriver,在我的应用程序中,我有两个标签。第一个选项更改第二个选项内的选项,并在onchange事件中启用它 当我使用Selenium2提供的Select对象时,它在IE8中运行时不会触发该事件(在FF和手动操作时效果很好) 第一个按预期更改。但是,第二个仍为空并处于禁用状态。我尝试将此作为一种解决方法: if(ie) { WebElement select = getElementByName(name); WebElement option = select.findElement(B

在我的应用程序中,我有两个
标签。第一个选项更改第二个选项内的选项,并在onchange事件中启用它

当我使用Selenium2提供的Select对象时,它在IE8中运行时不会触发该事件(在FF和手动操作时效果很好)

第一个
按预期更改。但是,第二个
仍为空并处于禁用状态。我尝试将此作为一种解决方法:

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
}
if(ie){
WebElement select=getElementByName(名称);
WebElement option=select.findElement(由.cssSelector(“[value=”“+value+”]);
列表选项=select.findElements(By.cssSelector(“选项”);
//选择第一个元素
选项。获取(0)。单击();
//确保选择的对象是焦点
选择。单击();//打开
选择。单击();//关闭
键盘键盘=getWebDriver().getKeyboard();
对于(int i=0;i
但现在我得到了不一致的结果。所需的
始终处于选中状态,而只有在某些情况下才会触发事件


显然,最好的选择是让Select在IE8中工作。还有其他人看到过这个问题吗?似乎是硒元素2中的一只虫子。是否有已知的解决方法?

看起来您已经实现了SelectElement类,所以您尝试过这个吗

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

看起来您已经实现了SelectElement类,所以您尝试过这个吗

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

Am使用以下代码在“国家”列表中选择一个值(一旦选择了“国家”值,相应的“州”列表正在加载):

WebElement selectCountry=driver.findElement(By.id(“国家”);
列表选项=selectCountry.findElements(按.tagName(“选项”));
用于(WebElement选项:选项){
if(option.getText().equalsIgnoreCase(“印度”)){
选项。单击();
打破
}
}

注意-与FF相比,此选择操作需要更多的时间。您可能需要使用driver.manage()增加命令超时时间。

Am使用以下代码在“国家”列表中选择一个值(一旦选择了“国家”值,相应的“状态”列表将加载):

WebElement selectCountry=driver.findElement(By.id(“国家”);
列表选项=selectCountry.findElements(按.tagName(“选项”));
用于(WebElement选项:选项){
if(option.getText().equalsIgnoreCase(“印度”)){
选项。单击();
打破
}
}

注意-与FF相比,此选择操作需要更多的时间。您可能需要使用driver.manage()增加命令超时时间。

在与#Selenium IRC聊天室中的一些Selenium用户交谈后,我确定了以下修复方法:

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

在#Selenium IRC聊天室中与一些Selenium人交谈后,我决定解决这个问题:

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

我还没有遇到过这个问题,但是我遇到了一些问题,其中Selenium大部分时间都在工作,但有时由于元素还没有出现在页面上,它大部分时间都失败了。我编写了一个方法,等待元素加载,然后重试。如果它再次失败,我会自动重新测试它是否失败。虽然不是最理想的,但它可以工作。我还没有遇到过这个问题,但我遇到过一些问题,其中Selenium在大多数情况下都可以工作,但有时由于元素尚未出现在页面上,所以它在大多数情况下都会失败。我编写了一个方法,等待元素加载,然后重试。如果它再次失败,我会自动重新测试它是否失败。不是最优的,但它是有效的。