How to stop execution of CasperJS and allow the user to enter some value, and then continue execution?

I use PhantomJS and CasperJS to automate some of my tasks. In one of the tasks, I need to manually specify the captcha lines before I can actually complete this task. For this problem, I can come up with a screenshot of the webpage, then manually check the captured image and save the captcha line to a text file. After that, I can use the file system module in CasperJS to read this value and continue this process. I want to know how best to perform such tasks.

+7
javascript captcha phantomjs automation casperjs
source share
1 answer

Due to the accurate / control flow of CasperJS compared to PhantomJS, this task is not easy.

1. Raise the approach (poll file)

Say there is a secondary program (type 1) that processes a CAPTCHA, receives an input, and writes a text file with a CAPTCHA input. All CasperJS can handle is write a screenshot of CAPTCHA to disk and wait for the file to be processed with β€œcollapsible” text.

var fs = require("fs"), captchaFile = "cfile.png", parsedFile = "pfile.txt"; casper.waitForCaptcha = function(captchaFile, parsedFile){ casper.then(function(){ this.captureSelector(captchaFile, "someSelectorOfTheCaptcha"); }); casper.waitFor(function check(){ return fs.exists(parsedFile); }, function then(){ // do something on time // check if correct... if (!correct) { fs.remove(captchaFile); fs.remove(parsedFile); this.waitForCaptcha(captchaFile, parsedFile); // Problem: the secondary process needs to sense that a new CAPTCHA is presented } }, function onTimeout(){ // do something when failed }, 60000); // 1min should suffice as a timeout return this; }; casper.start(url).waitForCaptcha(captchaFile, parsedFile).run(); 

This code assumes that you want to try again when the CAPTCHA is wrong, but not if the minute intentionally passed without a decoded file. This is the process of pulling by polling if files already exist.

2. Push approach

The push process is also possible when a secondary program (type 2) sends requests to the CasperJS process using the PhantomJS web server . Since there will be two parallel control flows, the CasperJS part should wait a long time, but as soon as a request with decoded words is received, the wait can be violated with unwait .

 var server = require('webserver').create(), fs = require("fs"), captchaFile = "cfile.png"; function neverendingWait(){ this.wait(5000, neverendingWait); } casper.checkCaptcha = function(captchaFile, phantomPort, secondaryPort){ // here the CAPTCHA is saved to disk but it can also be set directly if captured through casper.captureBase64 this.captureSelector(captchaFile, "someSelectorOfTheCaptcha"); // send request to the secondary program from the page context this.evaluate(function(file){ __utils__.sendAJAX("http://localhost:"+secondaryPort+"/", "POST", {file: file}, true); }, captchaFile); // start the server to receive solved CAPTCHAs server.listen(phantomPort, { 'keepAlive': true }, function (request, response) { console.log('Request received at ' + new Date()); if (request.post) { // is there a response? this.then(function(){ // check if it is correct by reading request.post ... if (!correct){ response.statusCode = 404; response.headers = { 'Cache': 'no-cache', 'Content-Type': 'text/plain;charset=utf-8' }; response.close(); server.close(); this.checkCaptcha(captchaFile, phantomPort, secondaryPort); } else { response.statusCode = 200; response.headers = { 'Cache': 'no-cache', 'Content-Type': 'text/plain;charset=utf-8' }; response.close(); server.close(); this.unwait(); // abort the neverendingWait } }); } else { response.statusCode = 404; response.headers = { 'Cache': 'no-cache', 'Content-Type': 'text/plain;charset=utf-8' }; response.close(); server.close(); this.checkCaptcha(captchaFile, phantomPort, secondaryPort); } }); return this; }; casper.start(url).then(function(){ this.checkCaptcha(captchaFile, 8080, 8081); }).then(neverendingWait).then(function(){ // Do something here when the captcha is successful }).run(); 
+12
source share

All Articles