firstpostcommenter
firstpostcommenter

Reputation: 2921

HtmlUnit form submit since button does not have a direct hyperlink

I have a button on a page but there is no hyperlink in the button. So I need to submit the form to go to the next page.

HtmlUnit is not waiting till the next page loads. So the nextPage variable is having the current page instead of the next page (intermittently it works if page loads quick enough though). How to resolve this?

Html Page:

    <form action="/webapp/NewPage.jsp" id="idForm01" accept-charset="UNKNOWN" onsubmit="return false;" 
    name="frmNewPageForm" method="post" enctype="application/x-www-form-urlencoded">
                   <input name="attribute1" type="HIDDEN" value="1">

                   <input id="attribute2" value="EDIT_ATTRIBUTE2" name="functionalAttribute" type="hidden">

                </form>


                <table id="idTablePageDetails" class="formTable">

                   <tbody>
                    <tr class="formHeaderRow">
                      <td colspan="4" nowrap="">
                         Page Details

                         <a onclick="onClick_newPageButton();return false;" id="newPageButton" title="New Page" href="#" class="smallButton">
        <img id="image" border="0" class="smallButtonImg" src="/webapp/image/imageButton.png"></a>

                         <script type="text/javascript" language="Javascript">
                        <!--
                        function onClick_newPageButton() {
                           buttonAction_newPageButton();
                        }
                        function buttonAction_newPageButton() {   
            if ( allowClick() == true ) { addFormField( "frmNewPageForm", "action", "ACTION_NEW_PAGE" );
            if ( document.frmNewPageForm.onsubmit != null ) { document.frmNewPageForm.onsubmit(); }document.frmNewPageForm.submit(); }}
                        // -->
                        </script>

</table>

Code I am using to get the next page:

HtmlPage nextPage = (HtmlPage) page.executeJavaScript("document.frmNewPageForm.onsubmit()").getNewPage(); // from onsubmit

Upvotes: 0

Views: 274

Answers (3)

RBRi
RBRi

Reputation: 2879

Your observation is the result of the today common ajax/async/javascript based one page approach for web applications.

You can use a code pattern like this

    ... find the clickable element...
    myBtn.click();
    webClient.waitForBackgroundJavaScript(10000);

    HtmlPage resultPage = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();

After the click you have to wait until the async started javascript is processed. Then you have to get access to the current page (the content of the current window) because the page is usually replaced during the javascript processing.

There is also a method waitForJobsStartingBefore() if you like to have more control over the wait time. Both methods are returning before the given timeframe in case there is no more javascript job pending. For more details have a look at the javadoc and the code.

If you like to have a look at a more sophisticated usage of the api you can have a look at the Wetator (https://www.wetator.org/) source code. A good starting point is the class HtmlUnitBrowser (https://wetator.repositoryhosting.com/trac/wetator_wetator/browser/trunk/wetator/src/org/wetator/backend/htmlunit/HtmlUnitBrowser.java)

Hope that helps.

Upvotes: 1

firstpostcommenter
firstpostcommenter

Reputation: 2921

Append any attributes to the form if you need using NameValuePair.

Then Call onSubmit() and then Submit()

HtmlPage nextPage1 = (HtmlPage) page.executeJavaScript("document.frmNewPageForm.onsubmit()").getNewPage(); // from onsubmit
HtmlPage nextPage2 = (HtmlPage) nextPage1.executeJavaScript("document.frmNewPageForm.submit()").getNewPage(); // from submit

Upvotes: 0

laaf
laaf

Reputation: 131

Why do you do clicks on buttons or execute javascript instead do requests? I think this is more trustworthy

Upvotes: 0

Related Questions