2011-09-19 94 views
2

選擇使用硒2中選擇框不能選擇項目的Firefox(或鉻)驅動程序不能選擇從選擇框中

<select id="activations_month" name="activations[month]"> 
<option value="April 2011">April 2011</option> 
<option value="May 2011">May 2011</option> 
<option value="June 2011">June 2011</option> 
<option value="July 2011">July 2011</option> 
<option value="August 2011">August 2011</option> 
<option selected="selected" value="September 2011">September 2011</option> 
</select> 

選擇下拉=新選擇(sDriver.findElement(By.id(「activations_month」 ))); dropDown.selectByValue(「2011年8月」);

我試過按價值,按指數,按可見文本和取消選擇,所有隻是沒有做任何事情。我嘗試捕捉不趕它離開回到外面TestNG的測試運行,併到下一個方法

這確實做工精細異常,並返回正確的價值觀

List<WebElement> options = dropDown.getOptions(); 

System.out.println(options.size()); 
System.out.println(options.get(0).getText()); 
System.out.println(options.get(1).getText()); 
+0

更多信息:其實步入硒選擇代碼本身它未能找到在XPath檢查我的搜索字符串,它只要你進入不selectByVisibleText方法,那麼它似乎只是從InvocationTargetException跳出選擇的代碼,並且testNG接管了 – ducati1212

回答

0

這應該解決您的問題,不管它是什麼。不是最大的方式來選擇一個選項,但它應該工作:

List<WebElement> options = dropDown.getOptions(); 
for(WebElement option : options) 
{ 
    if(option.getAttribute("value").equals("August 2011")) 
    { 
     option.click(); 
     break; 
    } 
} 
+0

確實有效。我覺得奇怪的是,我必須這樣做,因爲硒似乎本質上希望讓您用較少的代碼進行選擇。反正謝謝,如果有人知道我爲什麼原來的代碼失敗讓我知道 – ducati1212

+0

嘿杜卡迪,通常你嘗試的方式確實工作。您正在使用的頁面可能存在一些問題。我已經看到了特定頁面的情況,其中selectbyvalue失敗,因爲即使它在瀏覽器中可見,根據硒元素也不是「可見的」。 – RedDeckWins

0

這可能不是理想的解決方案,但爲了保持這種簡單(並使其正常工作),您是否嘗試過實例化一個WebDriverBackedSelenium

driver = new FirefoxDriver(); 
selenium = new WebDriverBackedSelenium(driver, "your_url"); 

其次是傳統 -

selenium.select("id=activations_month", "label=May 2011"); 
selenium.select("id=activations_month", "label=June 2011"); 
+0

問題在於我已經導航到該頁面並完成了幾個步驟。所以我需要用硒支持的網絡驅動程序來編寫整個測試。不理想。如果有幫助,我認爲這是一個實際的硒缺陷。這工作了一段時間,以及這是我寫的代碼工作,我想說它打破了Firefox的更新,但我也更新了硒模塊。 – ducati1212

+0

f我嘗試通過索引選擇我在硒中選擇此代碼select boolean matched = false; (WebElement option:getOptions()){ if(match.equals(option.getAttribute(「index」))){ option.setSelected(); (!isMultiple()){return; } matched = true; } 我在我的代碼索引3索引 - > dropDown.selectByIndex(3); 我看到它一步一步,並在索引它匹配去option.setSelected();並失敗到 invocationTargetException這是不是在方法中處理,所以我完成了選擇並退出到測試運行器。思考? – ducati1212

+0

本文給出相當於Selenium RC命令 - http://rostislav-matl.blogspot.com/2011/03/moving-to-selenium-2-on-webdriver-part_26.html – rs79