EspenG
EspenG

Reputation: 537

Submitting form with HtmlUnit

Is there someone that know how to submit this form with HtmlUnit, Java?

    <form name="sendSmsForm" method="POST" action="/talkmore3/servlet/SendSmsFromSelfcare" onsubmit="return checkBeforeSubmit();">
      <input type="hidden" value name="message1" id="message1"/>
      <input type="hidden" value name="list" id="list"/>
     <table border="0" width="560">
        <tbody>
          <tr>
            <td>
              <div id="ContactListPanel">
                <iframe height="425" width="560" frameborder="0" scrolling="no" id="ContactListFrame" name="ContactListFrame" src="/talkmore3/servlet/ManageContactList">
                </iframe>
              </div>
            </td>
          </tr>
          <tr align="left">
            <td colspan="2" align="left">        
<div class="button_green" style="width: 114px; float: left;margin-left: 130px">
                <a href="javascript:sendSMS();" title="Send SMS">
                  <span>
                    Send SMS
                  </span>
                </a>
              </div>
            </td>
          </tr>
        </tbody>
      </table>
    </form>

I have tried with this:

HtmlForm form = page.getFormByName("sendSmsForm");
form.getInputByName("list").setValueAttribute("99999999");
form.getInputByName("message1").setValueAttribute("test");
HtmlAnchor submit = page.getFirstByXPath("//a[@href='javascript:sendSMS();']");
submit.click();

But it seems like it does not get the values of the input..

Upvotes: 0

Views: 3089

Answers (1)

Aron Bordin
Aron Bordin

Reputation: 436

As you can see, this html contains some js scripts, so you'll need to run it to get the desired behavior.

Try something like:

HtmlForm form = page.getFormByName("sendSmsForm");
form.getInputByName("list").setValueAttribute("99999999");
form.getInputByName("message1").setValueAttribute("test");

page = (HtmlPage) page.executeJavaScript("checkBeforeSubmit()").getNewPage(); // from onsubmit
page = (HtmlPage) page.executeJavaScript("sendSMS()").getNewPage(); //from submit link

this code should set the form values, validate the form and then do some action with the sendSMS() method.

Usually you need to track the js in these pages. Some of them listen input changes, do some kind of validation, etc. Also, take look in the checkBeforeSubmit and sendSMS functions to see what they are doing with your page.

Upvotes: 3

Related Questions