HtmlUnit表单提交,因为按钮没有直接超链接 [英] HtmlUnit form submit since button does not have a direct hyperlink

查看:89
本文介绍了HtmlUnit表单提交,因为按钮没有直接超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有一个按钮,但是按钮中没有超链接.因此,我需要提交表单以转到下一页.

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不会等待下一页加载.因此,nextPage变量具有当前页面而不是下一页(不过,如果页面加载得足够快,则可以间歇地工作).该如何解决?

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页面:

    <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

推荐答案

您的观察是当今针对Web应用程序基于ajax/async/javascript的一页方法的结果.

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();

单击后,您必须等待直到异步启动的javascript处理完毕.然后,您必须有权访问当前页面(当前窗口的内容),因为通常在javascript处理期间会替换该页面.

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.

如果您希望对等待时间有更多控制,还可以使用方法waitForJobsStartingBefore().如果没有更多的javascript作业正在等待,这两种方法都将在给定的时间范围之前返回.有关更多详细信息,请查看javadoc和代码.

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.

如果您想了解API的更复杂用法,可以看看Wetator( https://www.wetator.org/)源代码.一个很好的起点是HtmlUnitBrowser类( https://wetator.repositoryhosting.com/trac/wetator_wetator/browser/trunk/wetator/src/org/wetator/backend/htmlunit/HtmlUnitBrowser.java )

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)

希望有帮助.

这篇关于HtmlUnit表单提交,因为按钮没有直接超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆