2

I'm trying to login into Instagram with Puppeteer, but somehow I'm unable to do it.

Can you help me?

Here is the link I'm using:

https://www.instagram.com/accounts/login/

I tried different stuff. The last code I tried was this:

const puppeteer = require('puppeteer');

(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.instagram.com/accounts/login/');
await page.evaluate();

await afterJS.type('#f29d14ae75303cc', 'username');

await afterJS.type('#f13459e80cdd114', 'password');

await page.pdf({path: 'page.pdf', format: 'A4'});

await browser.close();
})();

Thanks in advance!

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
SenslessX
  • 23
  • 1
  • 1
  • 3

2 Answers2

13

OK you're on the right track but just need to change a few things.

  • Firstly, I have no idea where your afterJS variable comes from? Either way you won't need it.
  • You're asking for data to be typed into the username and password input fields but aren't asking puppeteer to actually click on the log in button to complete the log in process.
  • page.evaluate() is used to execute JavaScript code inside of the page context (ie. on the web page loaded in the remote browser). So you don't need to use it here.

I would refactor your code to look like the following:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://www.instagram.com/accounts/login/');
  await page.waitForSelector('input[name="username"]');
  await page.type('input[name="username"]', 'username');
  await page.type('input[name="password"]', 'password');
  await page.click('button[type="submit"]');
  // Add a wait for some selector on the home page to load to ensure the next step works correctly
  await page.pdf({path: 'page.pdf', format: 'A4'});
  await browser.close();
})();

Hopefully this sets you down the right path to getting past the login page!

Update 1:
You've enquired about parsing the text of an element on Instagram... unfortunately I don't have an account on there myself so can't really give you an exact solution but hopefully this still proves of some value.

So you're trying to evaluate an elements text, right? You can do this as follows:

const text = await page.$eval(cssSelector, (element) => {
  return element.textContent;
});

All you have to do is replace cssSelector with the selector of the element you wish to retrieve the text from.

Update 2:
OK lastly, you've enquired about scrolling down to an element within a parent element. I'm not going to steal the credit from someone else so here's the answer to that:

How to scroll to an element inside a div?

What you'll have to do is basically follow the instructions in there and get that to work with puppeteer similar to as follows:

await page.evaluate(() => {
  const lastLink = document.querySelectorAll('h3 > a')[2];
  const topPos = lastLink.offsetTop;

  const parentDiv = document.querySelector('div[class*="eo2As"]');
  parentDiv.scrollTop = topPos;      
});

Bear in mind that I haven't tested that code - I've just directly followed the answer in the URL I've provided. It should work!

AJC24
  • 3,280
  • 2
  • 19
  • 30
  • Thanks, that worked for me! Can you tell me how I can parse the evaluated site to text? – SenslessX Oct 25 '18 at 22:31
  • Could you give me an idea of what exactly you're trying to achieve (ie. why parse it to text)? Just so as I can give you the best answer for this? – AJC24 Oct 26 '18 at 06:47
  • I'm trying to parse the likers of my profile to text. – SenslessX Oct 26 '18 at 16:30
  • OK I've added a piece on how to get the text from an element in the DOM using puppeteer. I hope this helps you out - unfortunately I can't post an exact solution as I'm not active on instagram! – AJC24 Oct 26 '18 at 16:50
  • Thank you so much! One last question: how can I scroll in a container down? e.g. right her: (no login needed) https://www.instagram.com/p/BpRS13rh729/?taken-by=hotellopezdeharo how can i scroll down do the last? I could transfer this for the likes. – SenslessX Oct 26 '18 at 19:12
4

You can log in to Instagram using the following example code:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Wait until page has loaded

  await page.goto('https://www.instagram.com/accounts/login/', {
    waitUntil: 'networkidle0',
  });

  // Wait for log in form

  await Promise.all([
    page.waitForSelector('[name="username"]'),
    page.waitForSelector('[name="password"]'),
    page.waitForSelector('[name="submit"]'),
  ]);

  // Enter username and password

  await page.type('[name="username"]', 'username');
  await page.type('[name="password"]', 'password');

  // Submit log in credentials and wait for navigation

  await Promise.all([
    page.click('[type="submit"]'),
    page.waitForNavigation({
      waitUntil: 'networkidle0',
    }),
  ]);

  // Download PDF

  await page.pdf({
    path: 'page.pdf',
    format: 'A4',
  });

  await browser.close();
})();
Grant Miller
  • 27,532
  • 16
  • 147
  • 165