|
| 1 | +/** |
| 2 | + * @name Instagram |
| 3 | + * |
| 4 | + * @desc Logs into Instagram with credentials. Provide your username and password as environment variables when running the script, i.e: |
| 5 | + * `INSTAGRAM_USER=myuser INSTAGRAM_PWD=mypassword node instagram.js` |
| 6 | + * |
| 7 | + */ |
| 8 | +const puppeteer = require('puppeteer') |
| 9 | +const screenshot = 'instagram.png'; |
| 10 | +(async () => { |
| 11 | +const browser = await puppeteer.launch({ |
| 12 | +headless: false |
| 13 | +}) |
| 14 | +const page = await browser.newPage() |
| 15 | +await page.goto("https://www.instagram.com/accounts/login/?source=auth_switcher", { |
| 16 | +waitUntil: 'networkidle2' |
| 17 | +}); |
| 18 | + |
| 19 | +//email |
| 20 | +await page.waitForSelector("[name='username']"); |
| 21 | +// await page.click("[name='username']"); |
| 22 | +await page.type("[name='username']", process.env.INSTAGRAM_USER); |
| 23 | + |
| 24 | +//password |
| 25 | +await page.keyboard.down("Tab"); |
| 26 | +//uncomment the following if you want the passwor dto be visible |
| 27 | +// page.$eval("._2hvTZ.pexuQ.zyHYP[type='password']", (el) => el.setAttribute("type", "text")); |
| 28 | +await page.keyboard.type(process.env.INSTAGRAM_PWD); |
| 29 | + |
| 30 | +//the selector of the "Login" button |
| 31 | +// await page.click("._0mzm-.sqdOP.L3NKy>.Igw0E.IwRSH.eGOV_._4EzTm"); |
| 32 | + |
| 33 | +//we find the Login btn using the innerText comparison because the selector used for the btn might be unstable |
| 34 | +await page.evaluate(() => { |
| 35 | +let btns = [...document.querySelector(".HmktE").querySelectorAll("button")]; |
| 36 | +btns.forEach(function (btn) { |
| 37 | +if (btn.innerText == "Log In") |
| 38 | +btn.click(); |
| 39 | +}); |
| 40 | +}); |
| 41 | + |
| 42 | +//Optional |
| 43 | +//check if the element asking to download the app arises |
| 44 | +// try { |
| 45 | +// await loginPage.waitForSelector("._3m3RQ._7XMpj",{ |
| 46 | +// timeout:3000 |
| 47 | +// }); |
| 48 | +// await loginPage.click("._3m3RQ._7XMpj"); |
| 49 | +// } catch (err) { |
| 50 | + |
| 51 | +// } |
| 52 | + |
| 53 | +//Optional |
| 54 | +//check if the app asks for notifications |
| 55 | +// try { |
| 56 | +// await loginPage.waitForSelector(".aOOlW.HoLwm",{ |
| 57 | +// timeout:5000 |
| 58 | +// }); |
| 59 | +// await loginPage.click(".aOOlW.HoLwm"); |
| 60 | +// } catch (err) { |
| 61 | + |
| 62 | +// } |
| 63 | + |
| 64 | +await page.waitForSelector(".glyphsSpriteMobile_nav_type_logo"); |
| 65 | + |
| 66 | +await page.screenshot({ path: screenshot }); |
| 67 | + |
| 68 | +browser.close() |
| 69 | +console.log('See screenshot: ' + screenshot) |
| 70 | +})() |
0 commit comments