Skip to content

Instantly share code, notes, and snippets.

@rash0
Last active March 27, 2025 09:07
Show Gist options
  • Save rash0/74677d56eda8233a02d182b8947c2520 to your computer and use it in GitHub Desktop.
Save rash0/74677d56eda8233a02d182b8947c2520 to your computer and use it in GitHub Desktop.
Automate login to twitter with Puppeteer
const puppeteer = require("puppeteer");
const user_email = "[email protected]";
const user_handle = "@example"; //either your handle or phone number
const password = "theEndisNear";
async function fkTwitter() {
const browser = await puppeteer.launch({
headless: false,
});
const page = await browser.newPage();
await page.goto("https://twitter.com/i/flow/login");
await page.waitForNetworkIdle({ idleTime: 1500 });
///////////////////////////////////////////////////////////////////////////////////
// Select the user input
await page.waitForSelector("[autocomplete=username]");
await page.type("input[autocomplete=username]", user_email, { delay: 50 });
// Press the Next button
await page.evaluate(() =>
document.querySelectorAll('div[role="button"]')[2].click()
);
await page.waitForNetworkIdle({ idleTime: 1500 });
///////////////////////////////////////////////////////////////////////////////////
// Sometimes twitter suspect suspicious activties, so it ask for your handle/phone Number
const extractedText = await page.$eval("*", (el) => el.innerText);
if (extractedText.includes("Enter your phone number or username")) {
await page.waitForSelector("[autocomplete=on]");
await page.type("input[autocomplete=on]", user_handle, { delay: 50 });
await page.evaluate(() =>
document.querySelectorAll('div[role="button"]')[1].click()
);
await page.waitForNetworkIdle({ idleTime: 1500 });
}
///////////////////////////////////////////////////////////////////////////////////
// Select the password input
await page.waitForSelector('[autocomplete="current-password"]');
await page.type('[autocomplete="current-password"]', password, { delay: 50 });
// Press the Login button
await page.evaluate(() =>
document.querySelectorAll('div[role="button"]')[2].click()
);
await page.waitForNetworkIdle({ idleTime: 2000 });
// Either close the browser and kill the fun, OR make a baby bot to tweet instead of you
await browser.close();
}
fkTwitter();
@shafiqsaif
Copy link

Hi. I hope you're fine and doing well I would like to tell you that I'm not able to login because twitter has updated login details like first email after that clicking next page then password so I think that's why's it's not working. Can you help me please. I'll be thankful to you for this act of cooperation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment