在 htmlUnit 中执行 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8268143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:51:07  来源:igfitidea点击:

execute Javascript in htmlUnit

javascripthtmlunit

提问by irbash

I am trying to execute javascript using htmlUnit. After executing the javascript, I want to check if the page had some changes due to javascript execution. i.e I want to compare the html page before and after javascript execution...

我正在尝试使用 htmlUnit 执行 javascript。执行 javascript 后,我​​想检查页面是否因 javascript 执行而发生了一些更改。即我想比较 javascript 执行前后的 html 页面...

Any ideas how I can do it...

任何想法我怎么能做到...

Here's the sample code explaining what I actually intent to do...

这是解释我真正打算做什么的示例代码......

public static void main(String[] args) {
    final WebClient webClient = new WebClient();

    HtmlPage page;
    try {
        page = webClient
                .getPage("http://www.somepage.com");
        System.out.println(page.asXml());
        System.out.println(page.getByXPath("//script"));

        BufferedInputStream buffer = null;
        // System.out.print("getWebSite " + urlValue + "\n");

        URL url = new URL(
                "http://www.somepage.com/someJS.js");
        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        buffer = new BufferedInputStream(urlc.getInputStream());

        StringBuilder builder = new StringBuilder();
        int byteRead;
        while ((byteRead = buffer.read()) != -1)
            builder.append((char) byteRead);

        buffer.close();

        ScriptResult result = page.executeJavaScript(builder.toString());
        Object jsResult = result.getJavaScriptResult();

        HtmlPage afterExecution = (HtmlPage) result.getNewPage();

        System.out.println(afterExecution.asXml());

    } catch (FailingHttpStatusCodeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

回答by Rodney Gitzel

"Executing" the Javascript source won't do anything. I expect you get very little back from result.getNewPage(). Try changing the example to hit a real site, and explain what result you expect to see, then we can try executing your example.

“执行”Javascript 源代码不会做任何事情。我希望你从result.getNewPage(). 尝试更改示例以访问真实站点,并解释您希望看到的结果,然后我们可以尝试执行您的示例。

That said, one thing that might help you is to think of HtmlUnit as a browser you control via Java. You don't "run" the Javascript in a page, HtmlUnit runs it. You pretend you are a human user clicking on things, but you do the "clicking" via Java code.

也就是说,可能对您有帮助的一件事是将 HtmlUnit 视为您通过 Java 控制的浏览器。您不会在页面中“运行”Javascript,HtmlUnit 会运行它。您假装自己是点击事物的人类用户,但您通过 Java 代码进行“点击”。

In your example, you would navigate the DOM in your pageto find something that you use to trigger the Javascript -- perhaps clicking a button or an image. The result of calling click()will give you a new page resulting from whatever the Javascript did.

在您的示例中,您将在您的 DOM 中导航page以找到用于触发 Javascript 的内容——也许单击按钮或图像。调用的结果click()将为您提供一个新页面,无论 Javascript 做了什么。