我正在尝试自动在一个站点中填写表格。但问题是,该网站有验证码。因此,有什么办法可以显示登录页面时,在C#中像Console.ReadLine();
这样的命令提示符下手动输入验证码。 puppeteer中有任何类似的功能吗?
这是我的代码:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.setViewport({ width: 1366, height: 768});
await page.goto('https://www.irctc.co.in/nget/train-search');
await page.click('#loginText');
await page.type('#userId','xxxxxx');
await page.type('#pwd','xxxxxx');
await page.screenshot({path: 'varanjith.png'});
})();
屏幕截图:
答案 0 :(得分:0)
如果要在命令行中输入验证码,则可以使用内置的readline
模块
const puppeteer = require('puppeteer');
const readline = require('readline');
async function readLine() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(resolve => {
rl.question('Enter captcha: ', (answer) => {
rl.close();
resolve(answer)
});
})
}
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.setViewport({ width: 1366, height: 768});
await page.goto('https://www.irctc.co.in/nget/train-search');
await page.click('#loginText');
await page.type('#userId','xxxxxx');
await page.type('#pwd','xxxxxx');
const captcha = await readLine();
await page.type('#nlpAnswer', captcha)
await page.screenshot({path: 'varanjith.png'});
})();
原始答案:
如果您想在用户界面中输入验证码,并在填写后继续使用脚本,则可以使用:page.waitForFunction
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.setViewport({ width: 1366, height: 768});
await page.goto('https://www.irctc.co.in/nget/train-search');
await page.click('#loginText');
await page.type('#userId','xxxxxx');
await page.type('#pwd','xxxxxx');
console.log('Waiting for captcha');
await page.waitForFunction(() => {
const captchaInput = document.getElementById('nlpAnswer');
return captchaInput &&
captchaInput.value &&
captchaInput !== document.activeElement
})
console.log('Captcha filled');
await page.screenshot({path: 'varanjith.png'});
})();
故障:
该特定页面中的验证码输入为:'nlpAnswer'
const captchaInput = document.getElementById('nlpAnswer');
captchaInput
可用,它具有价值并且不再关注return captchaInput &&
captchaInput.value &&
captchaInput !== document.activeElement
答案 1 :(得分:0)
如@Estradiaz所述。您可以将readline与某些promise样板一起使用:
(async () => {
// Launches browser
let browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.setViewport({ width: 1366, height: 768});
await page.goto('https://www.irctc.co.in/nget/train-search');
await page.click('#loginText');
await page.type('#userId','xxxxxx');
await page.type('#pwd','xxxxxx');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let fulfill;
const answerPromise = new Promise(x => fulfill = x);
rl.question('Enter the Captcha: ', (answer) => {
fulfill(answer);
rl.close();
});
const answer = await answerPromise;
console.log(answer);
await page.type('#nlpAnswer',answer);
await page.waitFor(5000);
await page.screenshot({path: 'varanjith.png'});
browser.close();
})();