From: David N. <dav...@gm...> - 2014-03-15 06:26:38
|
On 3/14/14, Jack Bi <814...@qq...> wrote: > Hi all: > I found htmlUnit always failed when execute JS in html, and if I try > the > same code, sometimes it's success, I was puzzled for a long time. > My Java code like this, hope someone can help me solve this > problem,thank you very much. > > WebClient webClient = new > WebClient(BrowserVersion.FIREFOX_24); > webClient.addWebWindowListener(new WebWindowListener() > { > > public void webWindowOpened(WebWindowEvent event) { > > } > public void webWindowContentChanged(WebWindowEvent event) { > page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage(); > if (page.getTitleText().equals("Security Warning")) { > warningTag = true; > } > if (page.asText().contains(descTag)) { > finalTag = true; > } > } > public void webWindowClosed(WebWindowEvent event) { > > } > }); > > page = webClient.getPage(urlString); > for(int i = 0; i < 200; i++){ > if (warningTag || finalTag) { > break; > } > Thread.sleep(1000); > } > > the HTML code is like this: > <html> > <head> > <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> > <title> > title > </title> > <script type="text/javascript"> > window.setTimeout('document.getElementById("formxh4t").submit();',1000); > <form name="formxh4t" id="formxh4t" > action="http://k2.ddtuangou.com/daili/process.php?action=update" > method="post" onsubmit="return updateLocation(this);"> > <input name="u" type="hidden" class="textbox" id="input" > value="http://dongtaiwang.com" size="60"/> > <input type="hidden" name="type" value="0"/> > </form> > </body> > </html> I have found the Javascript engines used by HTMLUnit unable to properly execute JS as well. For my project I wound up having to switch to PhantomJS/CasperJS. I'd recommend giving it a try. I still use HTMLUnit for some things, and for those things it does a really nice job, but when it comes to executing Javascript I have to use something else. |