HtmlUnit form submit since button does not have a direct hyperlink

旧时模样 提交于 2019-12-13 20:31:05

问题


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

回答1:


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.




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/54438034/htmlunit-form-submit-since-button-does-not-have-a-direct-hyperlink

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