2012-04-25 84 views
0

所以我试图访问一个文本框,以便将框从默认的“选择客户”更改为“PE”。我的问题是元素被Htmlunit识别为HtmlTextInput而不是HtmlSelection。我已经知道它运行dojo使事情变得复杂一点。Htmlunit选择输入运行Dojo javascript包

这是我想要操纵的代码片段。

<form action="#" method="GET"> 
<span class="headlines">Customer:</span> 
<select id="orgSelect" 
name="orgSelect" 
dojoType="dijit.form.FilteringSelect" 
labelType="text" 
    style="width: 150px;visibility:hidden" 
autoComplete="true" 

<option value="__select__" selected>Select Customer</option> 
<option value="-1">**All Customers**</option> 

<option value="2396-1986">PCLP</option> 

<option value="3-1987">PE</option> 

<option value="8262-1988">PEA</option> 

我的代码是

List <HtmlForm> f= page.getForms(); 
HtmlTextInput ba = f.get(0).getInputByName("orgSelect"); 
ba.setValueAttribute("PE"); 

以下是文字版的样子。

客户: 选择CustomerPE

报告类别: 选择报告Category_ 选择 _

回答

0

不能直接设置HtmlSelect。

您只需在选择内选择好选项即可。

如:但是

HtmlSelect select = page.getHtmlElementById("myId"); 
for (HtmlOption o : select.getOptions()) { 
    if (o.getValueAttribute().contains("myValue")) { 
     select.setSelectedAttribute(o, true); 
    } 
} 
+0

感谢maxmax我仍然得到一个例外。 java.lang.ClassCastException:com.gargoylesoftware.htmlunit.html.HtmlTextInput无法转换为 com.gargoylesoftware.htmlunit.html.HtmlSelect 还有其他想法吗? 再次感谢一切。 – 2012-05-15 14:26:30

+0

HtmlSelect select =(HtmlSelect)page.getHtmlElementById(“myId”);当然你需要提供正确的ID。 – maxmax 2012-05-15 15:23:06

+0

它不是我找不到元素,我得到一个类抛出异常。它期望一个HtmlText而不是HtmlSelect,它说我不能施放它。这是我正在使用的代码。 HtmlSelect select =(HtmlSelect)page.getHtmlElementById(“orgSelect”); 如果你看看我原来的帖子,你可以看到选择表单的ID是正确的。还有其他建议吗?再次感谢。 – 2012-05-15 17:33:21