Created
July 10, 2023 16:07
-
-
Save flrent/a081444130728233430419b92804cd67 to your computer and use it in GitHub Desktop.
Login on twitter automatically via Puppeeter
This file contains 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
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