开发者

HtmlUnit, trying to get 2 forms, get exception at second form

开发者 https://www.devze.com 2023-03-31 12:04 出处:网络
Currently using HtmlUnit. Getting first login page is no problem, succesfully logging in, getting next page, \"clicking\" the link to get the \"MyDetails\" page.

Currently using HtmlUnit.

Getting first login page is no problem, succesfully logging in, getting next page, "clicking" the link to get the "MyDetails" page.

After getting "MyDetails" page, i want to get the same way as im getting the first login form.

Why i need to get the form is that i want to change the password, and the fields are in a form.

When im trying to get the second form, it gives me exception as follows:

com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[form] attributeName=[name] attributeValue=[form2]

Gives exception at this line of code:

HtmlForm form2 = page3.getFormByName("form2");

Note: the first form name is "form1" & second form name is "form2".

Is this a problem with HtmlUnit?

Code:

try {
    WebClient webclient = new WebClient(BrowserVersion.FIREFOX_3_6);
    HtmlPage page1 =   webclient.getPage("http://www.highveld.mobi/pages/clubvip/login.aspx");

    HtmlForm form = page1.getFormByName("form1");
    HtmlSubmitInput buttonLogin = form.getInputByName("cmdLogin");

    HtmlTextInput cellLogin = form.getInputByName("txtCellNumber");
    HtmlPasswordInput passLogin = form.getInputByName("txtLoginPassword");
    cellLogin.setValueAttribute(change);
    passLogin.setValueAttribute(oldPass);

    HtmlPage page2 = buttonLogin.click();

    HtmlAnchor link = page2.getAnchorByHref("updateprofile.as开发者_运维技巧px");
    HtmlPage page3 = link.click();

    System.out.println(page3.getUrl());
    HtmlForm form2 = page3.getFormByName("form2");


    HtmlPasswordInput pass = form2.getInputByName("txtPassword");
    HtmlPasswordInput passConfirm = form2.getInputByName("txtConfirmPassword");
    HtmlSubmitInput button = form2.getInputByName("cmdUpdate");

    pass.setValueAttribute(newPass);
    passConfirm.setValueAttribute(newPass);

    HtmlPage page4 = button.click();
}


First of all, please update to HtmlUnit 2.9 in case you are using an old version. Secondly, replace this with this:

System.out.println(page3.getUrl());
HtmlForm form2 = page3.getFormByName("form2");

With this:

System.out.println(page3.getUrl());
System.out.println(page3.asXml());
HtmlForm form2 = page3.getFormByName("form2");

And check for the existance of the form2 element, which I'm pretty sure it shouldn't be there as it is throwing a ElementNotFoundException.

I usually use XPath instead of getFormByName, you can give it a try too.

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消