Created
April 1, 2024 03:10
-
-
Save zayniddindev/10c92f5a924762767c71ed01e08427ad to your computer and use it in GitHub Desktop.
Playwright script to get OTP from Gmail
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * | |
| * @param {{page: import("playwright").Page, email: string, password: string}} | |
| */ | |
| async function getOtpFromGmail({ page, email, password }) { | |
| await page.goto("https://mail.google.com"); | |
| const emailInput = await page.waitForSelector("#identifierId"); | |
| await emailInput.fill(email); | |
| try { | |
| const emailNextButton = await page.waitForSelector("#identifierNext > div> button", { timeout: 2000 }); | |
| await emailNextButton.click(); | |
| } catch (error) { | |
| const emailNextButton = await page.waitForSelector("#identifierNext", { timeout: 2000 }); | |
| await emailNextButton.click(); | |
| } | |
| try { | |
| const passwordInput = await page.waitForSelector("#password div div input", { timeout: 4000 }); | |
| await passwordInput.fill(password); | |
| } catch (error) { | |
| const passwordInput = await page.waitForSelector("#password", { timeout: 4000 }); | |
| await passwordInput.fill(password); | |
| } | |
| try { | |
| const pasNextButton = await page.waitForSelector("#passwordNext > div> button", { timeout: 2000 }); | |
| await pasNextButton.click(); | |
| } catch (error) { | |
| const pasNextButton = await page.waitForSelector("#passwordNext", { timeout: 2000 }); | |
| await pasNextButton.click(); | |
| } | |
| try { | |
| await page.waitForURL(/https:\/\/gds\.google\.com/, { timeout: 5000 }); | |
| const spanText = "Not now"; | |
| const xpathExpression = `//span[text()="${spanText}"]`; | |
| const result = document.evaluate(xpathExpression, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); | |
| const span = result.singleNodeValue; | |
| span.click(); | |
| } catch (error) {} | |
| await page.waitForURL("https://mail.google.com/mail/u/0/#inbox"); | |
| const emails = await page.$$("span[email='<provider email>']"); | |
| await emails[1].click(); | |
| const spans = await page.$$("span"); | |
| let otp_span; | |
| for (const span of spans) { | |
| if ((await span.innerText()).includes("Your verification code is as mentioned below")) { | |
| otp_span = span; | |
| break; | |
| } | |
| } | |
| const text = await otp_span.innerText(); | |
| const regex = /\b(\d{6})\b/; | |
| const match = text.match(regex); | |
| return match ? match[1] : null; | |
| } | |
| module.exports = { getOtpFromGmail }; | |
| // test | |
| (async () => { | |
| const { firefox } = require("playwright"); | |
| const browser = await firefox.launch({ headless: false }); | |
| const context = await browser.newContext(); | |
| const page = await context.newPage(); | |
| const otp = await getOtpFromGmail({ page, email: "[email protected]", password: "examplepassword" }); | |
| console.log(otp); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment