2017-12-21 20 views
0

javascript에 의해 생성 된 렌더링 된 HTML에 액세스해야합니다. javascript에 의해 생성되는 html이 inspect에 의해 보여지는 것처럼 보이지만 나는 페이지의 viewource에서 사용할 수 없습니다. 그래서 나는 그 html을 얻을 필요가있다. 나는 htmlunit로 노력하고있다. HTML의 유닛 2.12 받는다는 종속성htmlunit을 사용하여 Javascript로 생성 된 html에 액세스하는 방법은 무엇입니까?

나는 예외를

출력을 얻고을 사용

import java.io.IOException; 
    import java.net.MalformedURLException; 
    import com.gargoylesoftware.htmlunit.*; 
    import com.gargoylesoftware.htmlunit.html.HtmlPage; 
    /* 
    * To change this license header, choose License Headers in Project Properties. 
    * To change this template file, choose Tools | Templates 
    * and open the template in the editor. 
    */ 

    /** 
    * 
    * @author biznis 
    */ 


     public class JsoupImageEx { 

      public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException { 
      java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF); /* comment out to turn off annoying htmlunit warnings*/ 

      WebClient webClient = new WebClient(); 
      String url = ""; 
      System.out.println("Loading page now: "+url); 
      HtmlPage page = webClient.getPage(url); 
      webClient.waitForBackgroundJavaScript(30 * 1000); /* will wait JavaScript to execute up to 30s */ 

      String pageAsXml = page.asXml(); 
      System.out.println(pageAsXml); 

      } 
     } 

는 :

Loading page now: 
    Exception in thread "main" ======= EXCEPTION START ======== 
    EcmaError: lineNumber=[2] column=[0] lineSource=[<no source>] name=[TypeError] sourceName=[https://inapi.ankiti.com/api/mms.bulksms101.com/www/parts/jquery-2.2.4.min.js?field=content] message=[TypeError: Cannot find function addEventListener in object [object HTMLDocument]. (https://inapi.ankiti.com/api/mms.bulksms101.com/www/parts/jquery-2.2.4.min.js?field=content#2)] 
    com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function addEventListener in object [object HTMLDocument]. (https://inapi.ankiti.com/api/mms.bulksms101.com/www/parts/jquery-2.2.4.min.js?field=content#2) 
     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:669) 
     at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:601) 
     at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507) 
     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:555) 
     at com.gargoylesoftware.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:1082) 
     at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:399) 
     at com.gargoylesoftware.htmlunit.html.HtmlScript$3.execute(HtmlScript.java:260) 
     at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:276) 
     at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:676) 
     at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source) 
     at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:635) 
     at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1170) 
     at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1072) 
     at org.cyberneko.html.filters.DefaultFilter.endElement(DefaultFilter.java:206) 
     at org.cyberneko.html.filters.NamespaceBinder.endElement(NamespaceBinder.java:330) 
     at org.cyberneko.html.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3074) 
     at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2041) 
     at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:918) 
     at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:499) 
     at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:452) 
     at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) 
     at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:892) 
     at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:241) 
     at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:187) 
     at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:268) 
     at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:156) 
     at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:434) 
     at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:309) 
     at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:374) 
     at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:359) 
     at JsoupImageEx.main(JsoupImageEx.java:25) 
    Caused by: net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot find function addEventListener in object [object HTMLDocument]. (https://inapi.ankiti.com/api/mms.bulksms101.com/www/parts/jquery-2.2.4.min.js?field=content#2) 
     at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3603) 
     at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3587) 
     at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3608) 
     at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3624) 
     at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3688) 
     at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2207) 
     at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2189) 
     at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1333) 
     at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798) 
     at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105) 
     at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:405) 
     at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:275) 
     at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3031) 
     at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:115) 
     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$3.doRun(JavaScriptEngine.java:546) 
     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:654) 
     ... 30 more 
    Enclosed exception: 
    net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot find function addEventListener in object [object HTMLDocument]. (https://inapi.ankiti.com/api/mms.bulksms101.com/www/parts/jquery-2.2.4.min.js?field=content#2) 

     at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2207) 
     at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2189) 
     at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1333) 
     at script(https://inapi.ankiti.com/api/mms.bulksms101.com/www/parts/jquery-2.2.4.min.js?field=content:2) 
     at script(https://inapi.ankiti.com/api/mms.bulksms101.com/www/parts/jquery-2.2.4.min.js?field=content:2) 
     at script(https://inapi.ankiti.com/api/mms.bulksms101.com/www/parts/jquery-2.2.4.min.js?field=content:2) 
     at script(https://inapi.ankiti.com/api/mms.bulksms101.com/www/parts/jquery-2.2.4.min.js?field=content:2) 
     at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798) 
     at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105) 
     at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:405) 
     at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:275) 
     at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3031) 
     at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:115) 
     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$3.doRun(JavaScriptEngine.java:546) 
     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:654) 
     at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:601) 
     at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:507) 
     at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:555) 
     at com.gargoylesoftware.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:1082) 
     at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:399) 
     at com.gargoylesoftware.htmlunit.html.HtmlScript$3.execute(HtmlScript.java:260) 
     at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:276) 
     at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:676) 
     at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source) 
     at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:635) 
     at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1170) 
     at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1072) 
     at org.cyberneko.html.filters.DefaultFilter.endElement(DefaultFilter.java:206) 
     at org.cyberneko.html.filters.NamespaceBinder.endElement(NamespaceBinder.java:330) 
     at org.cyberneko.html.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3074) 
     at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2041) 
     at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:918) 
     at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:499) 
     at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:452) 
     at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) 
     at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:892) 
     at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:241) 
     at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:187) 
     at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:268) 
     at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:156) 
     at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:434) 
     at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:309) 
     at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:374) 
     at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:359) 
     at JsoupImageEx.main(JsoupImageEx.java:25) 
    ======= EXCEPTION END ======== 

난 그 outerHTML에 렌더링 얻을 필요가있다.

+0

웹 클라이언트 .getOptions()를 사용해 보셨습니까? setThrowExceptionOnScriptError (false); – PillHead

+0

네, 지금 출력물을 얻고 있습니다.하지만 출력 된 HTML을 인쇄하지 않는 scripterror를 사용하고 있습니다. 어떻게해야할까요? –

답변

0

HtmlUnit 2.12가 2013 년 3 월 6 일에 출시되었습니다.이 버전이 아직 지원 되리라 예상합니까? 우리는 지금 2.28에 있고 2.29는 도중에있다. 멀리 내가 Changes에서 빠른 모양에서 볼 수있는이 버그는 2.14에서 해결 될 수 있지만 확실하지 않습니다.

최신 버전으로 업데이트하십시오. 이와 같은 문제가 여전히 발생하면 HtmlUnit에 대한 문제를 엽니 다.

그리고 마지막으로 복사하지 좋은 생각이 전혀이다

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF); /* comment out to turn off annoying htmlunit warnings*/ 

같은 유래에서 생각한다. 당신은 버그를 찾고 전체 로그를 비활성화합니다. 일반적으로 로그는 귀하와 다른 사람들이 문제를 찾기 위해 유용한 정보를 제공 할 수 있도록 도와줍니다.

+0

또는 jsoup에서 document.body.outerhtml()을 어떻게 사용할 수 있습니까? –

+0

2.28을 사용해 보셨습니까? – RBRi