Playwright 的 Twilio 一次性密码:Node.js(使用 github 的 2FA 自动登录)
Twilio One Time Password with Playwright: Node.js (automating login with github's 2FA)
我正在尝试使用 Playwright 自动执行 Github 的双因素身份验证。
第 47 行是我卡住的地方(在 // 获得 OTP 之后 ..)const element = await page.waitforselector(() => {
有解决办法吗?在这里的任何帮助表示赞赏。此代码通过 twilio 收到的消息检索 github 的 2 因素授权代码,然后应将其输入 'otp' 选择器。
const accountSid = 'examplesid';
const authToken = 'exampleauthtoken';
const client = require('twilio')(accountSid, authToken);
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({
headless: false
});
const context = await browser.newContext();
// Open new page
const page = await context.newPage();
// Go to https://github.com/login
await page.goto('https://github.com/login');
// Click input[name="login"]
await page.click('input[name="login"]');
// Fill input[name="login"]
await page.fill('input[name="login"]', 'usernameexample');
// Press Tab
await page.press('input[name="login"]', 'Tab');
// Fill input[name="password"]
await page.fill('input[name="password"]', 'passwordexample*');
// Press Enter
await page.press('input[name="password"]', 'Enter');
// assert.equal(page.url(), 'https://github.com/sessions/two-factor');
// Click [placeholder="6-digit code"]
await page.click('[placeholder="6-digit code"]');
// get OTP ...
const element = await page.evaluate(() => {
return new Promise((resolve, reject) => {
try {
client.messages.list({limit: 1 })
.then((messages) =>
messages.forEach((m) => {
console.log(m.body);
resolve(m.body);
})
);
} catch (error) {
reject (error);
}
});
});
// ---------------------
await context.close();
await browser.close();
})();
我得到的错误:
unhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:23523) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
已解决。删除了 Const 元素和承诺:
// get OTP (one time password)
const messages = await client.messages.list({limit: 1});
const number = messages[0].body.substr(0,6);
//Fill OTP
await page.fill('input[name="otp"]', number);
await page.press('input[name="otp"]', 'Enter');
我正在尝试使用 Playwright 自动执行 Github 的双因素身份验证。
第 47 行是我卡住的地方(在 // 获得 OTP 之后 ..)const element = await page.waitforselector(() => {
有解决办法吗?在这里的任何帮助表示赞赏。此代码通过 twilio 收到的消息检索 github 的 2 因素授权代码,然后应将其输入 'otp' 选择器。
const accountSid = 'examplesid';
const authToken = 'exampleauthtoken';
const client = require('twilio')(accountSid, authToken);
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({
headless: false
});
const context = await browser.newContext();
// Open new page
const page = await context.newPage();
// Go to https://github.com/login
await page.goto('https://github.com/login');
// Click input[name="login"]
await page.click('input[name="login"]');
// Fill input[name="login"]
await page.fill('input[name="login"]', 'usernameexample');
// Press Tab
await page.press('input[name="login"]', 'Tab');
// Fill input[name="password"]
await page.fill('input[name="password"]', 'passwordexample*');
// Press Enter
await page.press('input[name="password"]', 'Enter');
// assert.equal(page.url(), 'https://github.com/sessions/two-factor');
// Click [placeholder="6-digit code"]
await page.click('[placeholder="6-digit code"]');
// get OTP ...
const element = await page.evaluate(() => {
return new Promise((resolve, reject) => {
try {
client.messages.list({limit: 1 })
.then((messages) =>
messages.forEach((m) => {
console.log(m.body);
resolve(m.body);
})
);
} catch (error) {
reject (error);
}
});
});
// ---------------------
await context.close();
await browser.close();
})();
我得到的错误:
unhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:23523) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
已解决。删除了 Const 元素和承诺:
// get OTP (one time password)
const messages = await client.messages.list({limit: 1});
const number = messages[0].body.substr(0,6);
//Fill OTP
await page.fill('input[name="otp"]', number);
await page.press('input[name="otp"]', 'Enter');