2017-06-04 3 views
1

これはproblameticあるコードの一部です:CasperJS - DOMメソッド内部で実行されませんが(評価)機能

var obj = {}; 


function putInObject() { 
    obj.title = document.querySelector('[itemprop="title"]').innerText; 
    obj.description = document.querySelector('[itemprop="description"]').innerText; 
} 


casper.then(function(){ 
    casper.wait(1000,function(){ 

     links = this.evaluate(getItemLinks); 

     casper.each(links, function(self, link) { 
      self.thenOpen(link, function() { 

       this.echo(this.getTitle()); 

       this.wait(7000, function(){ 
        console.log("**************** \n WebPage is loaded \n ****************"); 

        // Appearantly, The putInObject() doesn't run and "obj" remains null 

        casper.evaluate(putInObject); 
        this.echo(obj); 

       }); 

      }); 
     }); 
    }); 
}); 

putInObject()機能は、私はこれでcasper.evaluate(putInObject);を置き換えしても、実行されません。 :

this.evaluate(function() { 
    obj.title = document.querySelector('[itemprop="title"]').innerText; 
    obj.description = document.querySelector('[itemprop="description"]').innerText; 
}); 

しかし、それもうまくいかず、objは[object Object]のようにコンソールに出力されます。

しかし、this.echo(this.getTitle());はうまく動作し、コンソールのページのタイトルを表示します。

この上記のコードのログ:

[debug] [phantom] opening url: https://example.com/category/section/, HTTP GET 
[debug] [phantom] Navigation requested: url=https://example.com/category/section/, type=Other, willNavigate=true, isMainFrame=true 
[debug] [phantom] url changed to "https://example.com/category/section/" 
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false 
[debug] [phantom] Navigation requested: url=https://staticxx.facebook.com/connect/xd_arbiter/r/0F7S7QWJ0Ac.js?version=42#channel=f1413c20e7ccaa&origin=https%3A%2F%2Fexample.com, type=Other, willNavigate=true, isMainFrame=false 
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false 
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false 
[debug] [phantom] Successfully injected Casper client-side utilities 
[info] [phantom] Step anonymous 12/28 https://example.com/category/section/ (HTTP 200) 
WebPage's Title: Section Name 
[info] [phantom] Step anonymous 12/28: done in 81055ms. 
[info] [phantom] Step _step 13/29 https://example.com/category/section/ (HTTP 200) 
[info] [phantom] Step _step 13/29: done in 81075ms. 
[info] [phantom] wait() finished waiting for 7000ms. 
[info] [phantom] Step anonymous 14/30 https://example.com/category/section/ (HTTP 200) 
**************** 
WebPage is loaded 
**************** 
[object Object] 

答えて

1

page.evaluateは我々と同じではない別の世界へのポータルのようなものです。 という名前のオブジェクトですが、objという名前ですが、CasperJsスクリプトの開始時にobjと宣言されているのと同じではありません。これらは異なるオブジェクトです。 page.evaluateで何が起こるかは、page.evaluateにとどまります。特にに返信する必要がある場合を除きます。

​​
+0

ありがとう、Vaviloff。私はあなたが言うことを得て、あなたのヒントを使用しましたbutlはthis.echo(obj);から 'null'を得ました。修正されたコードは次のとおりです:https://pastebin.com/96D3dSVs。 'console.log(JSON.stringify(obj)); 'について、私はウェブページのコンテキストで使うべきだと思うので、' return'ステートメントの前に 'putInObject()'関数の中に入れます。私の端末(casperJSログ) – DummyBeginner

+0

私の悪い、Webページの 'obj'オブジェクトを初期化するのを忘れてしまった。更新されたコードを参照してください(PhantomJSの生の方言ではありますが)実際の例があります:https://pastebin.com/Yvhxe2cp – Vaviloff

関連する問題