Menu

#1807 <body onload="document.forms[0].submit()"> not work for 2.22

2.22
closed
1
2016-07-14
2016-07-14
jiangchuan
No

Hi Sir,
I have a page like this:

<html> 
    xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head></head>
    <body onload="document.forms[0].submit()">
        <noscript>
            <p>
                <strong>Note:</strong> Since your browser does not support JavaScript,
                you must press the Continue button once to proceed.

            </p>
        </noscript>
 <form action="****.com" method="post">
 <noscript>
                <div>
                    <input type="submit" value="Continue"/>
                </div>
            </noscript>
        </form>
    </body>
</html>

HtmlUnit 2.22 can not execute the onload and I can not submit the form. Is there anyway to submit the form?
Thanks.

Discussion

  • Ahmed Ashour

    Ahmed Ashour - 2016-07-14
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,6 +1,7 @@
     Hi Sir,
     I have a page like this:
    
    +~~~
     <html> 
         xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
         <head></head>
    @@ -21,6 +22,7 @@
             </form>
         </body>
     </html>
    +~~~
    
     HtmlUnit 2.22 can not execute the onload and I can not submit the form. Is there anyway to submit the form?
     Thanks.
    
     
  • Ahmed Ashour

    Ahmed Ashour - 2016-07-14

    I changed the first two lines to be:

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

    And I successfully get:

    com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 404 Not Found for http://localhost:8080/****.com

    Please provide your java code and revise/edit the HTML in question.

     
  • jiangchuan

    jiangchuan - 2016-07-14

    Hi Ashour,

    Thanks for your quick reply. It is strange that Htmlunit can redirect in your test. My Java code is like this:

    ~~~
    try (final WebClient webClient = (proxyconfig.getHost() == null) ? new WebClient(BrowserVersion.FIREFOX_45)
    : new WebClient(BrowserVersion.FIREFOX_45, proxyconfig.getHost(), proxyconfig.getPort()))
    {
    webClient.getOptions().setUseInsecureSSL(true);
    // try to avoid Javascript errors & Exceptions
    webClient.getOptions().setRedirectEnabled(true);
    webClient.getOptions().setPrintContentOnFailingStatusCode(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.setJavaScriptTimeout(30000);
    webClient.waitForBackgroundJavaScript(3000);
    webClient.getOptions().setJavaScriptEnabled(false);
    currentPage = webClient.getPage(url);
    }
    ~~~

    In the currentPage, the page is the origin page(did not redirect), the value of currentPage.getBody() is HtmlBody[]. So it did not execute the onload() and did not redirect in my side. Is there any thing wrong?

     

    Last edit: Ahmed Ashour 2016-07-14
  • Ahmed Ashour

    Ahmed Ashour - 2016-07-14
    • status: open --> closed
    • assigned_to: Ahmed Ashour
     
  • Ahmed Ashour

    Ahmed Ashour - 2016-07-14

    Please remove:

            webClient.getOptions().setJavaScriptEnabled(false);
    
     
  • jiangchuan

    jiangchuan - 2016-07-14

    I remove the code but still can not redirect automatically. The current code is :

    String url = "http://localhost:8080/DemoApp/test.jsp";
    try (final WebClient webClient = (proxyconfig.getHost() == null) ? new WebClient(BrowserVersion.FIREFOX_45)
    : new WebClient(BrowserVersion.FIREFOX_45, proxyconfig.getHost(), proxyconfig.getPort()))
    {
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getOptions().setRedirectEnabled(true);
    webClient.getOptions().setPrintContentOnFailingStatusCode(true);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.setJavaScriptTimeout(30000);
    currentPage = webClient.getPage(url);
    }
    

    The currentPage still remain in http://localhost:8080/DemoApp/test.jsp, and did not redirect to ****.com.

     

    Last edit: Ahmed Ashour 2016-07-14
  • Ahmed Ashour

    Ahmed Ashour - 2016-07-14

    How can you know it didn't redirect?

    Please remove all .getOptions() setter statments, and you will have the excption thrown.

     
  • jiangchuan

    jiangchuan - 2016-07-14

    I'd like to give more detailed information about my test.

    I have a test.jsp deployed in tomcat, the url is http://localhost:8080/DemoApp/test.jsp, it redirect to http://localhost:8080/DemoApp/index.jsp automatically, the content is:

    <html> 
        <xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
        <head></head>
        <body onload="document.forms[0].submit()">
            <noscript>
                <p>
                    <strong>Note:</strong> Since your browser does not support JavaScript,
                    you must press the Continue button once to proceed.
                </p>
            </noscript>
     <form action="http://localhost:8080/DemoApp/index.jsp" method="post">
     <noscript>
                    <div>
                        <input type="submit" value="Continue"/>
                    </div>
                </noscript>
            </form>
        </body>
    </html>
    

    The java code is:

    public HtmlPage redirectPage(final String url) throws IOException, AuthenticationException
        {
            final ProxyConfig proxyconfig = Config.getInstance().getProxyConfig();
            HtmlPage currentPage = null;
            try (final WebClient webClient = (proxyconfig.getHost() == null) ? new WebClient(BrowserVersion.FIREFOX_45)
                : new WebClient(BrowserVersion.FIREFOX_45, proxyconfig.getHost(), proxyconfig.getPort()))
            {
                if (proxyconfig.getUserName() != null)
                {
                    final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider();
                    credentialsProvider.addCredentials(proxyconfig.getUserName(), proxyconfig.getUserPassword());
                    webClient.setCredentialsProvider(credentialsProvider);
                }
               webClient.getOptions().setUseInsecureSSL(true);
                webClient.getOptions().setRedirectEnabled(true);
                webClient.getOptions().setPrintContentOnFailingStatusCode(true);
                webClient.getOptions().setThrowExceptionOnScriptError(false);
                webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
                currentPage = webClient.getPage(url);
                System.out.println(currentPage.asXml());
            }
            return currentPage;
        }
    

    I know it did not redirect because it is still printing test.jsp, not index.jsp. In my understanding, if it redirected, the currentPage should be content of index.jsp. Then I can use forms and buttons on index.jsp.
    When I remove all .getOptions(), I get the following exceptions:

       Exception in thread "main" ======= EXCEPTION START ========
    Exception class=[java.lang.NullPointerException]
    com.gargoylesoftware.htmlunit.ScriptException
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:904)
        at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:515)
        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:835)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:807)
        at com.gargoylesoftware.htmlunit.InteractivePage.executeJavaScriptFunctionIfPossible(InteractivePage.java:216)
        at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeEventHandler(EventListenersContainer.java:295)
        at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:315)
        at com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget.fireEvent(EventTarget.java:206)
        at com.gargoylesoftware.htmlunit.html.DomElement$2.run(DomElement.java:1180)
        at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:515)
        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
        at com.gargoylesoftware.htmlunit.html.DomElement.fireEvent(DomElement.java:1185)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1213)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:272)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:518)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:385)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:303)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:450)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:435)
        at com.siemens.isb.test.utils.webservice.browserclients.AppRouterClient.redirectPage(AppRouterClient.java:594)
        at com.siemens.isb.test.utils.webservice.browserclients.AppRouterClient.main(AppRouterClient.java:555)
    Caused by: java.lang.NullPointerException
        at net.sourceforge.htmlunit.corejs.javascript.Delegator.getPrototype(Delegator.java:173)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject.getProperty(ScriptableObject.java:1619)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1437)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1423)
        at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:3027)
        at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:2487)
        at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
        at com.gargoylesoftware.htmlunit.javascript.host.event.EventHandler.call(EventHandler.java:82)
        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:398)
        at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:252)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3065)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:828)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:889)
        ... 21 more
    Enclosed exception: 
    java.lang.NullPointerException
        at net.sourceforge.htmlunit.corejs.javascript.Delegator.getPrototype(Delegator.java:173)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptableObject.getProperty(ScriptableObject.java:1619)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1437)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1423)
        at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:3027)
        at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:2487)
        at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
        at com.gargoylesoftware.htmlunit.javascript.host.event.EventHandler.call(EventHandler.java:82)
        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:398)
        at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:252)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3065)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:828)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:889)
        at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:515)
        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:835)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:807)
        at com.gargoylesoftware.htmlunit.InteractivePage.executeJavaScriptFunctionIfPossible(InteractivePage.java:216)
        at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeEventHandler(EventListenersContainer.java:295)
        at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:315)
        at com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget.fireEvent(EventTarget.java:206)
        at com.gargoylesoftware.htmlunit.html.DomElement$2.run(DomElement.java:1180)
        at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:515)
        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507)
        at com.gargoylesoftware.htmlunit.html.DomElement.fireEvent(DomElement.java:1185)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1213)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:272)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:518)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:385)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:303)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:450)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:435)
        at com.siemens.isb.test.utils.webservice.browserclients.AppRouterClient.redirectPage(AppRouterClient.java:594)
        at com.siemens.isb.test.utils.webservice.browserclients.AppRouterClient.main(AppRouterClient.java:555)
    == CALLING JAVASCRIPT ==
    function () {
        [native code, arity=0]
    }
    
    ======= EXCEPTION END ========
    
     

    Last edit: jiangchuan 2016-07-14
  • Ahmed Ashour

    Ahmed Ashour - 2016-07-14

    I retested.

    Make index.jsp contain simple text, e.g. "Hello", and you will have XML containing "Hello".

    The error you now see is because of something in index.jsp, which you need to send it for further investigations, or you provide a minimal test case.

     
    • jiangchuan

      jiangchuan - 2016-07-14

      Are you using my java code to do the test? It is strange that you get a different result with me.

       
  • jiangchuan

    jiangchuan - 2016-07-14

    The index.jsp is a simple page as follows:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
      <title>Welcome</title>
    
    </head>
    <body>
      <div class="textColumn">   
        <h1>Welcome</h1>
        <p>Your demo app is now running on your own dedicated environment in the AWS Cloud</p>
      </div>
    
    </body>
    </html>
    

    But the result for java code System.out.println(currentPage.asXml()) is

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <html>
      <head/>
      <body onload="document.forms[0].submit()">
        <xmlns>
          <noscript>
    
                &lt;p&gt;
                    &lt;strong&gt;Note:&lt;/strong&gt; Since your browser does not support JavaScript,
                    you must press the Continue button once to proceed.
    
                &lt;/p&gt;
    
          </noscript>
          <form action="http://localhost:8080/DemoApp/index.jsp" method="post">
            <noscript>
    
                    &lt;div&gt;
                        &lt;input type="submit" value="Continue"/&gt;
                    &lt;/div&gt;
    
            </noscript>
          </form>
        </xmlns>
      </body>
    </html>
    

    It is not index.jsp's content.

     
  • Ahmed Ashour

    Ahmed Ashour - 2016-07-14

    Please try with 23-SNAPSHOT, which you can also get from here.

    Code I use:

    HtmlPage currentPage = null;
    try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_45)) {
        String url = "http://localhost:8080/test.jsp";
        currentPage = webClient.getPage(url);
        System.out.println(currentPage.asXml());
    }
    
     

Log in to post a comment.

Monday.com Logo