最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - HtmlUnit 2.9 jar execute JavaScript - Stack Overflow

programmeradmin4浏览0评论

I am trying this code:

import .gargoylesoftware.htmlunit.BrowserVersion;
import .gargoylesoftware.htmlunit.JavaScriptPage;
import .gargoylesoftware.htmlunit.ScriptResult;
import .gargoylesoftware.htmlunit.WebClient;
import .gargoylesoftware.htmlunit.WebRequest;
import .gargoylesoftware.htmlunit.WebResponse;
import .gargoylesoftware.htmlunit.WebWindow;
import .gargoylesoftware.htmlunit.html.HtmlPage;
import .gargoylesoftware.htmlunit.javascript.JavaScriptEngine;
import .gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement;
import java.URL;
import java.util.List;

public class Example {

    public static void main(String[] args) throws Exception {

        WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6);
        URL url=new URL("");
        WebRequest request= new WebRequest(url);
        WebResponse response=new WebResponse(null, request, 6000);
        webClient.setJavaScriptEnabled(true);
        webClient.setThrowExceptionOnScriptError(false);
        webClient.setCssEnabled(false);

        webClient.setRedirectEnabled(true);

        JavaScriptEngine engine = new JavaScriptEngine(webClient);
        webClient.setJavaScriptEngine(engine);
        HtmlPage firstPage = null;
        ScriptResult result = null;
        JavaScriptPage jsp=new JavaScriptPage(response, null);
        try {
            firstPage = webClient.getPage(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String JavaScriptCode = "1+1";

        try {
           result = firstPage.executeJavaScript(JavaScriptCode);

        } catch (Exception e) {
            e.printStackTrace();
        }
        Object javaScriptResult = result.getJavaScriptResult();
        System.out.println(javaScriptResult);
    }
}

It works good with the simple JavaScript Code such as "1+1". i want to execute a particular function that is defined in the page source of the URL. URL is the field that i have defined in this code.

I am trying this code:

import .gargoylesoftware.htmlunit.BrowserVersion;
import .gargoylesoftware.htmlunit.JavaScriptPage;
import .gargoylesoftware.htmlunit.ScriptResult;
import .gargoylesoftware.htmlunit.WebClient;
import .gargoylesoftware.htmlunit.WebRequest;
import .gargoylesoftware.htmlunit.WebResponse;
import .gargoylesoftware.htmlunit.WebWindow;
import .gargoylesoftware.htmlunit.html.HtmlPage;
import .gargoylesoftware.htmlunit.javascript.JavaScriptEngine;
import .gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement;
import java.URL;
import java.util.List;

public class Example {

    public static void main(String[] args) throws Exception {

        WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6);
        URL url=new URL("http://www.google.");
        WebRequest request= new WebRequest(url);
        WebResponse response=new WebResponse(null, request, 6000);
        webClient.setJavaScriptEnabled(true);
        webClient.setThrowExceptionOnScriptError(false);
        webClient.setCssEnabled(false);

        webClient.setRedirectEnabled(true);

        JavaScriptEngine engine = new JavaScriptEngine(webClient);
        webClient.setJavaScriptEngine(engine);
        HtmlPage firstPage = null;
        ScriptResult result = null;
        JavaScriptPage jsp=new JavaScriptPage(response, null);
        try {
            firstPage = webClient.getPage(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String JavaScriptCode = "1+1";

        try {
           result = firstPage.executeJavaScript(JavaScriptCode);

        } catch (Exception e) {
            e.printStackTrace();
        }
        Object javaScriptResult = result.getJavaScriptResult();
        System.out.println(javaScriptResult);
    }
}

It works good with the simple JavaScript Code such as "1+1". i want to execute a particular function that is defined in the page source of the URL. URL is the field that i have defined in this code.

Share Improve this question edited Apr 12, 2013 at 4:23 Mallikarjuna Reddy 1,2122 gold badges20 silver badges33 bronze badges asked Apr 13, 2012 at 7:30 user1319054user1319054 411 silver badge5 bronze badges 1
  • please help me to call function that is defined in the pagesource of the url. Thanks. – user1319054 Commented Apr 13, 2012 at 7:44
Add a ment  | 

1 Answer 1

Reset to default 4

Here is a working example, I've tried to make it as simple as possible:

import .gargoylesoftware.htmlunit.BrowserVersion;
import .gargoylesoftware.htmlunit.WebClient;
import .gargoylesoftware.htmlunit.html.HtmlPage;

public class Test {
    public static void main(String[] args) throws Exception {

       WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);

       HtmlPage page = webClient.getPage("http://www.iana/");
       String javaScriptCode = "inArray([1,2],3)";

       Object result = page.executeJavaScript(javaScriptCode).getJavaScriptResult();
       System.out.println(result);
    }
}
发布评论

评论列表(0)

  1. 暂无评论