尝试使用 HTMLUnit 保存 html 文本框元素时出现类型转换异常
Type casting exception in trying to save html text box element using HTMLUnit
我一直在尝试创建一个 Java class 到 运行 的网站测试,但我在尝试输入用户名和密码时遇到了障碍该站点使用 HTMLUnit API。以下是我尝试基于我的代码的 html 部分:
<div class="inner_contain">
<table cellpadding="2" cellspacing="1" border="0" align="center">
<tr>
<form name="frm1" action="blank" onSubmit="defaultSubmit(); return false;" method="post">
<Td align="right"><strong>Username:</strong></Td>
<td><input type="text" maxlength="20" class="login_input" /></td>
</form>
</tr>
<tr>
<form name="frm2" action="blank" onSubmit="defaultSubmit(); return false;" method="post">
<Td align="right"><strong>Password:</strong></Td>
<td><input type="password" maxlength="20"
class="login_input" /></td>
</form>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" value="Login" class="submit" onclick="javascript:LoginSubmit('Login')" /></td>
</tr>
</table>
</div>
下面是该部分的代码片段:
final HtmlForm usernameForm = page.getElementByName("frm1");
final HtmlTextInput usernameInput = (HtmlTextInput) usernameForm.getByXPath("//input[@class='login_input' and @type='text']");
但是无论是否使用 HtmlTextInput 转换,我仍然会收到以下错误:
java.util.ArrayList cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlTextInput
如果能在提交用户凭据方面提供任何帮助,我们将不胜感激!
你只需要阅读the API doc:
public List<?> getByXPath(String xpathExpr)
Evaluates the specified XPath expression from this node, returning the matching elements
注意return类型,以及复数形式的用法。如果要获取单个元素,则获取 returned 列表的第一个元素,或者使用 getFirstByXPath()
:
public <X> X getFirstByXPath(String xpathExpr)
Evaluates the specified XPath expression from this node, returning the first matching element, or null if no node matches the specified XPath expression.
阅读文档后,一切都会简单得多。
我一直在尝试创建一个 Java class 到 运行 的网站测试,但我在尝试输入用户名和密码时遇到了障碍该站点使用 HTMLUnit API。以下是我尝试基于我的代码的 html 部分:
<div class="inner_contain">
<table cellpadding="2" cellspacing="1" border="0" align="center">
<tr>
<form name="frm1" action="blank" onSubmit="defaultSubmit(); return false;" method="post">
<Td align="right"><strong>Username:</strong></Td>
<td><input type="text" maxlength="20" class="login_input" /></td>
</form>
</tr>
<tr>
<form name="frm2" action="blank" onSubmit="defaultSubmit(); return false;" method="post">
<Td align="right"><strong>Password:</strong></Td>
<td><input type="password" maxlength="20"
class="login_input" /></td>
</form>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" value="Login" class="submit" onclick="javascript:LoginSubmit('Login')" /></td>
</tr>
</table>
</div>
下面是该部分的代码片段:
final HtmlForm usernameForm = page.getElementByName("frm1");
final HtmlTextInput usernameInput = (HtmlTextInput) usernameForm.getByXPath("//input[@class='login_input' and @type='text']");
但是无论是否使用 HtmlTextInput 转换,我仍然会收到以下错误:
java.util.ArrayList cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlTextInput
如果能在提交用户凭据方面提供任何帮助,我们将不胜感激!
你只需要阅读the API doc:
public List<?> getByXPath(String xpathExpr)
Evaluates the specified XPath expression from this node, returning the matching elements
注意return类型,以及复数形式的用法。如果要获取单个元素,则获取 returned 列表的第一个元素,或者使用 getFirstByXPath()
:
public <X> X getFirstByXPath(String xpathExpr)
Evaluates the specified XPath expression from this node, returning the first matching element, or null if no node matches the specified XPath expression.
阅读文档后,一切都会简单得多。