I'm trying for the first time PhantomJs. My goal is simple simple simple page automation.
I only need to - load a web page, let's say www.google.com - fill the query term - simply (really, only this) console.log of search result page
I'm having some problem because all of tutorials I've found starts with too complicated task.
What follows is my personal step by step tutorial, but this 'ends' with a question: "how to log the content of page after submitting ?"
The first STEP I've done is:
var page = require('webpage').create();
page.open('https://www.google.it', function () {
console.log (page.content);
phantom.exit()
});
And this works.
Then I inject jquery
var page = require('webpage').create();
page.open('https://www.google.it', function () {
page.includeJs("http://code.jquery.com/jquery-1.10.1.min.js", function() {
console.log (page.content);
phantom.exit()
});
});
An that's working.
Now I'm trying to fill the search box. I must fill up this field
<input autocomplete="off" class="lst" value="" title="Cerca con Google" maxlength="2048" name="q" size="57" style="...[omitted]...">
I'm doing this:
var page = require('webpage').create();
page.open('https://www.google.it', function () {
page.includeJs("http://code.jquery.com/jquery-1.10.1.min.js", function() {
console.log (page.content);
var value = page.evaluate(function() {
$('input[name="q"]').val("Daduu");
return $('input[name="q"]').val();
});
console.log ("search term: " + value);
phantom.exit()
});
});
And it's working. Now I need to 'click' on submit button
<input class="lsb" value="Cerca con Google" name="btnG" type="submit">
Doing this:
var page = require('webpage').create();
page.open('https://www.google.it', function () {
page.includeJs("http://code.jquery.com/jquery-1.10.1.min.js", function() {
var value = page.evaluate(function() {
$('input[name="q"]').val("Daduu");
$('input[name="btnG"]').trigger("click");
return $('input[name="q"]').val();
});
// console.log (page.content);
page.render("google.png");
phantom.exit()
});
});
When I see the image ( just to see rendered results in a human-way), I see the google.it page with filled 'q' field.
The questions are:
**Is really the page loading [is it Google searching] ? **
"How to detect page change to console.log or page.render the content ?"
EDIT: I see this: Phantomjs - How to populate a form, submit and get the results?
But this is NOT what I need. I want to detect when page loading is complete, not to 'settimeout' to render page.
EDIT 2: I see this: PhantomJS: submit a form
But this is too complicated, I've not understood how this works.
Probably it's needed to wrap all around a setTimeout, but, because of using js objects, i'm looking for a way to work event-driven
onInitialPageLoaded (
fillForm
click
)
onSearchResultLoaded (
render or log content of page
)
But I cannot understand if is it possible in PhantomJs, and, at all, how to realize it !