2013-12-09 41 views
0

我試着去從下拉list.But選擇API選擇日期值失敗選擇api webdriver失敗的API?

Select date=new Select(driver.findElement(By.id("dropdown_day"))); 
date.selectByIndex(5); 

代碼片段是

<div id="dropdown_dropdown_day" class="dropdown" role="application" style="width: 32%;"> 
<input id="dropdown_day" type="hidden" value="0" name="dropdown_day"> 
<input id="btn_dropdown_day" class="btn-m btn-ter dropdown-toggle dropdown-input" type="text" style="min-height: 19px; width: 100%;" aria-haspopup="true" name="dp_dropdown_day" aria-owns="options_dropdown_day" aria-disabled="" role="combobox" readonly="readonly" title="" value="Day"> 
<i class="caret-dn-dp" aria-haspopup="true"></i> 
<ul id="options_dropdown_day" class="dropdown-menu-dp" style="min-width: 60px; width: 100%;" aria-hidden="true" aria-activedescendant="" role="listbox"> 
<li class="op" aria-selected="true" tabindex="0" role="option" op-value="0">Day</li> 
<li class="op" aria-selected="false" tabindex="-1" role="option" op-value="1">01</li> 
<li class="op" aria-selected="false" tabindex="-1" role="option" op-value="2">02</li> 
<li class="op" aria-selected="false" tabindex="-1" role="option" op-value="3">03</li> 
<li class="op" aria-selected="false" tabindex="-1" role="option" op-value="3">...</li> 
<li class="op" aria-selected="false" tabindex="-1" role="option" op-value="4">31</l 
</ul> 
+0

什麼是失敗錯誤? – irla

+0

線程「main」中的異常org.openqa.selenium.support.ui.UnexpectedTagNameException:元素應該是「選擇」但是是「輸入」 構建信息:版本:'2.37.0',修訂版本:'a7c61cb',時間:'2013-10-18 17:15:02' os.name:'Windows Vista',os.arch:'x86',os.version:'6.0',java.version:'1.7.0_45' 驅動程序info:driver.version:unknown \t at org.openqa.selenium.support.ui.Select。 (Select.java:46) –

回答

0

你的「選擇」是輸入。 Html select將是<select>

看起來像一些JavaScript正在將您的輸入轉換爲select。如果您使用HtmlUnitDriver,請啓用JavaScript或嘗試使用FirefoxDriver。

HtmlUnitDriver driver = new HtmlUnitDriver(); 
driver.setJavascriptEnabled(true); 

在調用您的代碼之前,您可能還需要等待一秒或一半的JavaScript執行。

+0

WebElement we = driver.findElement(By.id(「btn_dropdown_day」)); JavascriptExecutor executor =(JavascriptExecutor)驅動程序; executor.executeScript(「arguments [0] .click();」,we);通過使用上述JavaScript下拉列表已被點擊,但無法從列表中選擇值(日) –