2017-08-05 11 views
0

最初に<span>を選択し、もう一度<span>を入力し、そのinnerTextを取得します。 enter image description hereCasperJSで特定の要素を選択する方法は?

Bur私はxpathまたはdocument.querySelectorを使用しようとすると、機能しません。私console.log私の配列はnullを表示します。

// I want to select first <span> 
function getDate() { 
    var date = document.querySelector('div.movie_intro_info_r li').selectedIndex = 1; 
    return Array.prototype.map.call(date, function (e) { 
     return e.innerHTML; 
    }); 
}; 
// I want to select second <span> 
function getMovieLength() { 
    var movieLength = document.querySelectorAll(x('//*[@class="movie_intro_info_r"]/span[2]')); 
    return Array.prototype.map.call(movieLength, function (e) { 
     return e.innerText; 
    }); 
}; 

casper.then(function() { 
    casper.each(movieHref, function (casper, url) { 
     casper.thenOpen(url, function() { 
      casper.waitForSelector('div.btn_gray_info.gabtn', function() { 
       console.log('wait for element'); 
      }); 
      releaseDate[urlCount] = this.evaluate(getDate); 
      console.log(releaseDate[urlCount]);// show null 

      movieLength[urlCount] = this.evaluate(getMovieLength); 
      console.log(movieLength[urlCount]);// show null 
      urlCount++; 
     }); 
    }); 
}); 

特定の要素を選択してそのinnerTextを取得するにはどうすればよいですか?

答えて

2

あなたのコードは、あなたがやろうとしているためには少し複雑すぎます。あなたは、CSSセレクタでfetchTextメソッドを使用して検討する必要があります。

var casper = require('casper').create(); 

casper.start('YOUR_URL', function() { 
    this.echo(this.fetchText('.movie_intro_info_r > span:first-of-type')); 
    this.echo(this.fetchText('.movie_intro_info_r > span:nth-of-type(2)')); 
}).run(); 
+0

おかげでお返事のためのあなたは本当に、私はstackoverflowのからのAPIや他の人のコードを参照してくださいfetchTextあなたのコードのグーグルを試してみてください。私はまだ何も印刷することはできません、私は私の質問にしようとするコードを更新します。あなたは私のコードをチェックして、私はそれをmiisするべきですか?どうもありがとうございました ! –

+0

私はそれが存在するかどうかを確認しようとするかどうか '(this.exists('。movie_intro_info_r> span:nth-​​of-type(2) ')){ console.log(' yes '); } else { console.log( 'no'); } ' 印刷番号。 –

+0

今は問題ありません。あなたの提案から間違った場所にコードを置くからです。あなたの助け、祝福に感謝します。 –

関連する問題