Skip to content

Instantly share code, notes, and snippets.

@bnadland
Created April 16, 2014 18:42
Show Gist options
  • Save bnadland/10918913 to your computer and use it in GitHub Desktop.
Save bnadland/10918913 to your computer and use it in GitHub Desktop.
var page = require('webpage').create();
page.open('http://phantomjs.org', function(status) {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
console.log(page.evaluate(function() {
return $('a')
.map(function() {
return this.href;})
.get()
.join();
}));
phantom.exit()
});
});
@agarwalvinit
Copy link

hey How could I write this in simple javascript ?
when i wrote this -
try {
var urls = page.evaluate(function () {
var mappedurls = document.querySelectorAll('a')
.map(function () {
return this.href;
})
.get()
.join('/r/n');
return mappedurls;
});
console.log(urls);
} catch (e) {
console.log(e);
}

I am getting an error:
undefined is not a function (evaluating 'document.querySelectorAll('a')
.map(function () {
return this.href;
})')

phantomjs://webpage.evaluate():3
phantomjs://webpage.evaluate():9
null

@bnadland
Copy link
Author

Hi @agarwalvinit, I created this snippet to share it with a coworker of mine at a time when we were working on a crawler with http://phantomjs.org/ and it has been a while since I last worked in that area. That just as info on the origin of that code.

As for your question, I am not entirely sure I understand what you want to do. How did you run your code and what do you want to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment