如何在Java中使用HtmlUnit?

发布于 2024-10-15 08:48:24 字数 1180 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天煞孤星 2024-10-22 08:48:24

以下是 HTMLunit 单元测试的代码。

final HtmlSelect select = form.getSelectsByName("select1").get(0);
final List<HtmlOption> expected = new ArrayList<HtmlOption>();
expected.add(select.getOptionByValue("option1"));
expected.add(select.getOptionByValue("option3"));

请注意,他们使用 getSelectsByName 而不是 getElementById。

以下是这些单元测试的链接,以便您可以了解它们如何使用 API 进行规定。 http://htmlunit.sourceforge.net/xref-test /com/gargoylesoftware/htmlunit/html/HtmlSelectTest.html

Here's code from the unit tests for HTMLunit.

final HtmlSelect select = form.getSelectsByName("select1").get(0);
final List<HtmlOption> expected = new ArrayList<HtmlOption>();
expected.add(select.getOptionByValue("option1"));
expected.add(select.getOptionByValue("option3"));

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

听风念你 2024-10-22 08:48:24

获取登录用户名和密码的形式。

这是一个例子:

   HtmlPage page3;
   page3 = webClient.getPage("Website");
   HtmlForm loginForm = page3.getFormByName("loginForm");
   HtmlTextInput username = loginForm.getInputByName("NameofUsernameElement");
   HtmlPasswordInput pass = loginForm.getInputByName("NameofPassowordElement");
   HtmlSubmitInput b = loginForm.getInputByValue("LoginButtonValue");

   username.setValueAttribute("Actualy Username");
   pass.setValueAttribute("Actual Password");
   HtmlPage page2;
   page2 = b.click();

Get the form of the login username and password.

here is an example:

   HtmlPage page3;
   page3 = webClient.getPage("Website");
   HtmlForm loginForm = page3.getFormByName("loginForm");
   HtmlTextInput username = loginForm.getInputByName("NameofUsernameElement");
   HtmlPasswordInput pass = loginForm.getInputByName("NameofPassowordElement");
   HtmlSubmitInput b = loginForm.getInputByValue("LoginButtonValue");

   username.setValueAttribute("Actualy Username");
   pass.setValueAttribute("Actual Password");
   HtmlPage page2;
   page2 = b.click();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文