2012-09-23 10 views
7

Betiğin kendisinden oluşturulan URL'lere gitmeye çalışıyorum.Casperjs, URL'leri dinamik olarak açamıyor?

Bu örnek kod, beklediğim gibi çalışmıyor. :(

var casper = require('casper').create({ 
    viewportSize:{ 
     width:1024, height:768 
    }, 
    pageSettings:{ 
     userAgent:'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11' 
    }, 
    verbose:true 
}); 

casper.on('open', function (location) { 
    console.log(location + ' loaded'); 
}); 

casper.start('http://www.google.com', function() { 
    this.test.assertTitle('Google', 'Google homepage title is the one expected'); 
}); 

casper.mytest = ''; 

casper.then(function() { 
    casper.mytest = 'http://www.yahoo.com'; 
}); 

casper.thenOpen(casper.mytest, function() { 
    this.test.assertTitle('Yahoo', 'Yahoo homepage title is the one expected'); 
}); 

casper.run(function() { 
     casper.exit(); 
    } 
); 

sonuç ikinci sayfa değil yük olduğunu anlamaya Can not: Bence

http://www.google.com loaded 
PASS Google homepage title is the one expected 
loaded  
FAIL Yahoo homepage title is the one expected 
# type: assertTitle 
# subject: "" 
# expected: "Yahoo" 

cevap

10

, senin sorunun nedeni şu anda, ne zaman kayıt olmasıdır Yahoo değişken casper.mytest için thenOpen adım Bu değer şu anda adımların CasperJS haritasında içine alır. boş olduğunu ve daha önce adımda kaynak değişkeni değiştirmek önemli değil.

blog yazısı Webscraping with CasperJS and PhantomJS yararlı olabilir bir Dinamik olarak oluşturulmuş URL'leri almanın bir örneği.

+1

Teşekkür ederiz! Gerçekten de, eğer this.open() öğesini o zaman içinde() {} kullanırsam çalışır. Yine de çok rahatsız edici olsa da, projem ölçeklendiğinde muhtemelen çok çirkin kodlara yol açacağından korkuyorum :( – johnjohn

+0

@johnjohn, bir süre önce çalışmayı sağlayan CasperJS'den bir yama (küçük bir şey değil) yaptım adım adım (ad-hoc) olarak, muhtemelen sizin ihtiyaçlarınız için uygun olanı yapabilirsiniz. Benim sürümümdeki sorun, bir süre önce olduğu ve şu anda modası geçmiş olduğu ve limanı planlamamasıdır. Bu en son CasperJS'ye. – Stan