2011-12-01 121 views
1

在我的應用程序中,我有兩個<select>標籤。第一個更改第二個選項中的選項,並在onchange事件中啓用它。Selenium2的選擇對象不會在IE8中觸發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>總是被選中,而有時只有事件被觸發。

很明顯,最好的選擇是讓選擇在IE8中工作。有其他人看過這個問題嗎?看起來像Selenium2中的一個錯誤。有沒有已知的解決方法?

+0

我都沒有這個確切的問題,但我有問題,其中硒工作的最時間,但有時由於該元素尚未出現在頁面上而大部分時間都會失敗。我寫了一個方法,等待元素加載,然後再試一次。如果再次失敗,我會自動重新測試失敗。不是最佳的,但它的工作原理。 –

回答

1

與一些#selenium IRC聊天室我在此修復程序解決的硒民間的交談後:

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

看起來你已經實現SelectElement類,以便讓你嘗試使用下面的代碼此

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

AM,選擇在「國家」列表中的值(一旦「國家」價值選擇,相應的「狀態'列表正在加載):

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; 
    } 
} 

注 - 與FF相比,此選擇操作需要更多時間IE。您可能需要使用driver.manage()來增加命令超時時間。

相關問題