我可以使用HtmlUnit监听资源加载事件吗? [英] Can i use HtmlUnit to listen for resource loading events?

查看:82
本文介绍了我可以使用HtmlUnit监听资源加载事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HtmlUnit来检测无法加载到网页上的资源(脚本,图像,样式表等).

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

我尝试过

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

无济于事.尽管有HtmlUnit日志记录,它似乎也不能处理CSS,图像或JS:

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

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

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>

是否可以使用HtmlUnit检测所有404的AJAX资源,CSS脚本和图像?

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

推荐答案

  • 对于AJAX,您必须等待此处.
  • HtmlUnit不会自动加载 HtmlLink HtmlImage ,出于性能原因.
  • 请在下面的代码段中将其全部打印出来.您可以使用 .getByXPath( )以获取所有元素的列表.
    • 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();
          }
      }
      

      这篇关于我可以使用HtmlUnit监听资源加载事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆