如何在Java中使用HtmlUnit?
我正在尝试使用 Java 中的 HtmlUnit 登录网站。首先我输入用户名然后密码。之后我需要从下拉框中选择一个选项。输入用户和密码似乎有效,但是当我尝试从下拉框中选择该项目时,出现错误。谁能帮我解决这个问题吗?我的代码如下:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
public class homePage {
public static void main(String[] args) throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("website name here");
HtmlElement usrname = page.getElementByName("username");
usrname.click();
usrname.type("myusername");
HtmlElement psswrd = page.getElementByName("password");
psswrd.click();
psswrd.type("mypassword");
HtmlSelect select = (HtmlSelect) page.getElementById("cmbProducts");
HtmlOption option = select.getOptionByValue("ITDirect");
select.setSelectedAttribute(option, true);
HtmlElement signin = page.getElementByName("SignIn");
signin.click();
System.out.println(page.getTitleText());
webClient.closeAllWindows();
}
}
I'm trying to use HtmlUnit in Java to log into a website. First i enter the user name then password. After that i need to select an option from a dropdown box. entering the user and password seemed to have worked but when i try to select the item from the drop down box i get errors. Can anyone help me fix this? My code is as follows:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
public class homePage {
public static void main(String[] args) throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("website name here");
HtmlElement usrname = page.getElementByName("username");
usrname.click();
usrname.type("myusername");
HtmlElement psswrd = page.getElementByName("password");
psswrd.click();
psswrd.type("mypassword");
HtmlSelect select = (HtmlSelect) page.getElementById("cmbProducts");
HtmlOption option = select.getOptionByValue("ITDirect");
select.setSelectedAttribute(option, true);
HtmlElement signin = page.getElementByName("SignIn");
signin.click();
System.out.println(page.getTitleText());
webClient.closeAllWindows();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是 HTMLunit 单元测试的代码。
请注意,他们使用 getSelectsByName 而不是 getElementById。
以下是这些单元测试的链接,以便您可以了解它们如何使用 API 进行规定。 http://htmlunit.sourceforge.net/xref-test /com/gargoylesoftware/htmlunit/html/HtmlSelectTest.html
Here's code from the unit tests for HTMLunit.
Notice that they use getSelectsByName not getElementById.
Here's a link to those unit tests so you can see how they prescribe using the API. http://htmlunit.sourceforge.net/xref-test/com/gargoylesoftware/htmlunit/html/HtmlSelectTest.html
获取登录用户名和密码的形式。
这是一个例子:
Get the form of the login username and password.
here is an example: