Skip to content

Instantly share code, notes, and snippets.

@flrent
Created July 10, 2023 16:07
Show Gist options
  • Save flrent/a081444130728233430419b92804cd67 to your computer and use it in GitHub Desktop.
Save flrent/a081444130728233430419b92804cd67 to your computer and use it in GitHub Desktop.
Login on twitter automatically via Puppeeter
const loginToTwitter = async (page, username, password) => {
await page.goto('https://twitter.com/login');
await page.waitForSelector('input[autocomplete="username"][type="text"]');
const usernameInput = await page.$('input[autocomplete="username"][type="text"]');
// Type the username slowly
for (let i = 0; i < username.length; i++) {
await usernameInput.type(username[i], { delay: 100 });
}
// Submit the username form
await Promise.all([
usernameInput.press('Enter'),
]);
// Wait for 3 seconds
await page.waitForTimeout(2000);
// Find the password input field
await page.waitForSelector('input[autocomplete="current-password"][type="password"]');
const passwordInput = await page.$('input[autocomplete="current-password"][type="password"]');
// Type the password slowly
for (let i = 0; i < password.length; i++) {
await passwordInput.type(password[i], { delay: 100 });
}
// Submit the password form
await Promise.all([
passwordInput.press('Enter'),
page.waitForNavigation()
]);
// Wait for 5 seconds
await page.waitForTimeout(5000);
};
module.exports = { loginToTwitter };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment