JQuery include in phantomjs is not working -
my code is:
var url = 'https://search.yahoo.com/', page = new webpage(), fs = require('fs'); page.settings.useragent = 'mozilla/5.0 (platform; rv:geckoversion) gecko/geckotrail appname/appversion'; page.onconsolemessage = function (msg) { console.log(msg); }; page.open(url, function(status) { if (status !== 'success') { console.log('unable access network'); phantom.exit(); return; } else { page.includejs("https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js", function() { page.evaluate(function() { $('#yschsp').val("ask question"); $(".sbb").click(); }); page.onloadfinished = function(status) { var content = page.content; fs.write('1.html', content, 'w'); console.log($('#link-1').val()); phantom.exit(); }; }); } }); jquery works perfect in page.evaluate not work in page.onloadfinished. error
can't variable: $
that means in function page.onloadfinished jquery not working. can not understand why?
since jquery loaded page context, can use there. function interfaces page context evaluate (and other evaluate functions).
so line
console.log($('#link-1').val()); must inside of evaluate callback. since have page.onconsolemessage event handler receive console message page context.
the other thing adding page.onloadfinished event handler after page has loaded isn't doing useful. can remove handler surrounding code since page load finished when page.open callback called.
if #link-1 not yet loaded, should either log value after static timeout (settimeout) or use waitfor.
Comments
Post a Comment