Sometimes, we want to submit a form using PhantomJS and JavaScript.
In this article, we’ll look at how to submit a form using PhantomJS and JavaScript.
How to submit a form using PhantomJS and JavaScript?
To submit a form using PhantomJS and JavaScript, we use CasperJS.
To install it, we run
npm install casperjs
after installing PhantomJS.
Then we use it by writing
const casper = require("casper").create();
casper.start("http://example.com/login", function () {
this.fill(
"form#loginForm",
{
login: "admin",
password: "12345678",
},
true
);
this.evaluate(() => {
document.querySelector('input[type="submit"]').click();
});
});
We call casper.start
to open the page at the given URL.
And we call it with a function that calls this.fill
to get the form with the "form#loginForm"
selector.
And we send the values of the fields with the names as the keys with the values in the values.
Next, we call this.evaluate
with a callback that clicks the button after we filled the form.
Conclusion
To submit a form using PhantomJS and JavaScript, we use CasperJS.