Skip to content

Instantly share code, notes, and snippets.

@rash0
Last active July 6, 2024 14:52
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@example.com";
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();
@UmarSabeq
Copy link

@yekayee @ViennaMike @kaisugi I added the new script, it will work for now....

Can u please send me the last script

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