2013-03-17 21 views
1

我正在運行使用硒的webdriver如何點擊自動提示輸入文本框中的選項? webdriver的

測試硒我有一個問題與輸入文本框(「選擇來源:名稱:」)。 當我在文本框中輸入字符串「ABC Premium News(澳大利亞)」時,會顯示一個選項。然後我需要點擊(或選擇)它。我嘗試了所有方法w/fireEvent ...沒用。

以下是源代碼:

driver.get("http://www..."); 
driver.switchTo().frame("mainFrame"); 
WebElement sourceTitle = driver.findElement(By.name("sourceTitle")); 
sourceTitle.sendKeys("ABC Premium News (Australia)"); 
//Now a "combobox-like" option "ABC Premium News (Australia)" shows up...how do I click it? 

// I tried fireEvent...it did not help. The following is one of my trials that does not work: 
DefaultSelenium sel = new WebDriverBackedSelenium(driver,"http://www...."); 
sel.type("//input[@id='destination']", "ABC Premium News (Australia)"); 
sel.fireEvent("//input[@id='destination']", "keydown"); 
// In addition to keydown, I tried: onclick, onfocus, onblur... 
+0

看看這裏http://stackoverflow.com/questions/9202061/test-autocomplete-with-selenium-webdriver – 2013-03-17 05:33:44

+0

對不起@Amine Hajyoussef。我再次嘗試,似乎這種方法只幫助將值輸入到文本文件中,但是,它不會幫助選擇「類似組合框」選項。 ANYBODY,請運行上面的代碼行或訪問網站查看文本框。 – CHEBURASHKA 2013-03-17 06:15:58

+0

你能告訴我們html和截圖嗎? – 2013-03-18 07:34:45

回答

2

我知道這是不是最好的人能想出但仍可以作爲暫時的解決辦法。

WebElement sourceTitle = driver.findElement(By.name("sourceTitle")); 

WebElement small = driver.findElement(By.cssSelector("li#nameExampleSection label + small")); 

sourceTitle.sendKeys("ABC Premium News (Australia)"); 

Thread.sleep(5000); 

Actions actions = new Actions(driver); 

actions.click(small).perform(); 
+0

我編輯了答案。它應該工作。覈實。 :) – 2013-03-18 18:49:34

+0

謝謝@Code熱情! :) – CHEBURASHKA 2013-03-18 18:56:51

相關問題