How to use HtmlUnit in Java?

那年仲夏 提交于 2019-11-30 07:20:40

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

EK_AllDay

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();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!