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 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();
@ViennaMike
Copy link

The twitter login has changed so that you enter your id, then click "Next,", then enter your password on the next page. Like yekayee I'm having a hard time figuring out how to get playwright to click the Next button.

@rash0
Copy link
Author

rash0 commented May 15, 2023

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

@MylzEder
Copy link

so, thanks a lot for this, it works wonders, HOWEVER, it fails to choose the password and instead just dies, do you think there is any solution to that?

its gives me this error ```log
TimeoutError: Navigation timeout of 40000 ms exceeded
at LifecycleWatcher._LifecycleWatcher_createTimeoutPromise (C:\Users\Mylz\Documents\Puppeteer\node_modules\puppeteer-core\lib\cjs\puppeteer\common\LifecycleWatcher.js:158:12)
at async Deferred.race (C:\Users\Mylz\Documents\Puppeteer\node_modules\puppeteer-core\lib\cjs\puppeteer\util\Deferred.js:87:20)
at async Deferred.race (C:\Users\Mylz\Documents\Puppeteer\node_modules\puppeteer-core\lib\cjs\puppeteer\util\Deferred.js:87:20)
at async Frame.waitForNavigation (C:\Users\Mylz\Documents\Puppeteer\node_modules\puppeteer-core\lib\cjs\puppeteer\common\Frame.js:126:23)
at async CDPPage.waitForNavigation (C:\Users\Mylz\Documents\Puppeteer\node_modules\puppeteer-core\lib\cjs\puppeteer\common\Page.js:481:16)
at async Promise.all (index 0)
at async fkTwitter (C:\Users\Mylz\Documents\Puppeteer\node_modules\logintesting1.js:21:3)

Node.js v19.1.0

@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