简体   繁体   English

在CasperJS中,如何等待JSON完成加载?

[英]in CasperJS, how do you wait for JSON to complete loading?

I am visiting a site that emits a (large) JSON response. 我正在访问一个发出(大)JSON响应的网站。 A click triggers the request: 单击将触发请求:

casper.then(function li10() {
    casper.click(SEARCH_BUTTON_CSS);
});

But according to my web proxy, the client closes the connection before receiving the entire response. 但是根据我的Web代理,客户端在收到整个响应之前先关闭连接。 I've tried waiting for the URL to appear. 我试图等待URL出现。 It waits for the URL as expected, but that doesn't appear to be sufficient: 它按预期的那样等待URL,但这似乎还不够:

casper.then(function li11() {
    casper.waitForUrl(/\/search-results\/p\?/,
                      function() { 
                          var search_url = casper.getCurrentUrl();
                          console.log('found search results, url = ' + search_url);
                      },
                      function() { 
                          console.log('failed to find search results');
                          casper.exit();
                      },
                      10000);
});

So: what is something dependable that I can wait for that will guarantee that the JSON code has completely loaded before proceeding to the next step? 所以:我可以等待的可靠内容是什么,可以确保在继续下一步之前JSON代码已完全加载?

I'm assuming that you would fill a search field, click a button and the JavaScript makes an Ajax request to receive a JSON response to then parse it and display the results. 我假设您将填写搜索字段,单击一个按钮,然后JavaScript发出Ajax请求以接收JSON响应,然后对其进行解析并显示结果。

casper.waitForUrl() is used to wait for the page URL to change to the specified one. casper.waitForUrl()用于等待页面URL更改为指定的URL。 It has nothing to do with resources that are loaded separately such as AJAX responses. 它与单独加载的资源(例如AJAX响应)无关。

You either need to 你要么需要

  • find out the specific URL that is requested for the search action and use casper.waitForResource() to wait for that specific resource or 找出搜索操作所需的特定URL,然后使用casper.waitForResource()等待该特定资源,或者
  • devise a specific selector that appears when the search data is parsed and injected into the page with casper.waitForSelector() . 设计一个特定的选择器,当使用casper.waitForSelector()解析搜索数据并将其注入页面时,该选择器就会出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM