2011-08-26 129 views
1

我正在使用WATIN來完成一個動態創建的表單,該表單可以包含多個SelectLists(這些是javascript控制的選取列表)。輸出的選擇列表格式的樣本是;Watin無法從選擇列表中選擇一個選項

<select title=" " style="width: 300px;" 
    name="NameHere" 
    data-bind="value: NameHere.AnswerCode" 
    class="fieldInputElement pickList"> 
    <option selected="" value=""></option> 
    <option class="answerTextWithNote" value="A">alpha</option> 
    <option class="answerTextWithNote" value="B" data-guidance="E.g. minor ">bravo</option> 
    <option class="answerTextWithNote" value="C" data-guidance="E.g. b">charlie</option> 
    <option class="answerTextWithNote" value="C" data-guidance="E.g. c">chatlie</option> 
</select> 

不幸的是,watin似乎無法選擇列表和任何選項,返回錯誤。

代碼

​​

返回有關指數的誤差,雖然

string y = window.SelectList(Find.ByName("NameHere")).Option(x[1].ToString()).ToString(); 

將分配用於索引選項正確的值。

任何人都可以告訴我如何觸發選擇,因爲我已經嘗試了焦點()和keydown都沒有喜悅。

如果這有幫助,selectlist正在使用'Chosen'插件?

回答

1

以下是有點不漂亮....但是...它的工作原理。

使用

我無法重現你的索引錯誤;我可以通過索引訪問SelectList選項而不會出現問題(請參閱下面的代碼)。但是......訪問他們並沒有幫助,因爲Chosen有自己的演示標記。

所以,相反,我使用選擇的HTML元素而不是SelectList,事情工作得更好。

[Test] 
public void ChosenTest() 
{ 
    IE myIE = new IE(true); 
    myIE.GoTo("http://davidwalsh.name/dw-content/jquery-chosen.php"); 

    myIE.SelectList(Find.ByClass("chosen chzn-done")).WaitUntilExists(); //Needed as sometimes the controls were not fully loaded; unable to get item not found exceptions once this was added. 

    Console.WriteLine("ByIndex:" + myIE.SelectList(Find.ByClass("chosen chzn-done")).Options[3].Text); //To show no index out of bounds error. 
    // Just for reference --> myIE.SelectList(Find.ByClass("chosen chzn-done")).Options[3].Select(); //Has no effect. 

    string id = myIE.SelectList(Find.ByClass("chosen chzn-done")).Id; 
    myIE.Div(id + "_chzn").Div(Find.ByClass("chzn-drop")).ElementWithTag("li", Find.ByIndex(3)).Click(); 
    myIE.Div(id + "_chzn").Links[0].Spans[0].Click(); //Needed or the SelectList-ish stays open. 
} 

Finding ByClass是在示例頁面上的控件ID發生更改時完成的。 WaitUntilExists消除了間歇性故障。