Can i use HtmlUnit to listen for resource loading events?

时间秒杀一切 提交于 2020-01-05 03:00:08

问题


I'm trying to use HtmlUnit to detect resources (scripts, images, stylesheets, etc) that fail to load on a webpage.

I've tried

new WebConnectionWrapper(webClient) {
    @Override
    public WebResponse getResponse(WebRequest request) throws IOException {
        WebResponse response;
        response = super.getResponse(request);
        System.out.println(response.getStatusCode());
        return response;
    }
};

to no avail. It doesn't seem to handle CSS, images or JS, despite HtmlUnit logging:

statusCode=[404] contentType=[text/html] File: /resources/style.css

For reference, here's the file I'm loading:

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <script type='text/javascript'>
            var xhr = new XMLHttpRequest();
            xhr.open("get", "someBadLink", true);
            xhr.send();
        </script>
        <link href="/resources/style.css" rel="stylesheet"/>
    </head>

    <body>
        <img src="bad_image.png">
    Cool.
    </body>
</html>

Is there any way to use HtmlUnit to detect the AJAX resource, CSS script and image that all 404?


回答1:


  • For AJAX, you have to wait as hinted here.
  • HtmlUnit doesn't automatically loads HtmlLink and HtmlImage, for performance reasons.
  • Please find below snippet that will print all. You can use .getByXPath() to get list of all elements.


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

        new WebConnectionWrapper(webClient) {
            @Override
            public WebResponse getResponse(WebRequest request) throws IOException {
              WebResponse response = super.getResponse(request);
              System.out.println(request.getUrl());
              System.out.println(response.getStatusCode());
              return response;
            }
        };

        String url = "http://localhost/test.html";
        HtmlPage page = webClient.getPage(url);

        // to wait for AJAX
        webClient.waitForBackgroundJavaScript(3000);

        // to forcibly load the link
        HtmlLink link = page.getFirstByXPath("//link");
        link.getWebResponse(true);

        // to forcibly load the image
        HtmlImage image = page.getFirstByXPath("//img");
        image.getImageReader();
    }
}


来源:https://stackoverflow.com/questions/31253688/can-i-use-htmlunit-to-listen-for-resource-loading-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!