2011-08-26 138 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消除了间歇性故障。

相关问题