0

I am trying to send a DM on Instagram, but each new line is sent as one message.

Does type input and click not support multiple lines? If so, does anyone have any insight into alternative solutions, such as fixing this area of the puppeteer source code?

The simplified code is as follows. It may not work as it is because of the different language area. Sorry.

import pp from 'puppeteer';
import puppeteer from 'puppeteer-extra';

export const instagram = async () => {
  console.log('START!!!');

  const browser = await puppeteer.launch({
    executablePath: pp.executablePath(),
    args: [
      '--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
    ],
    headless: true,
    slowMo: 100,
    timeout: 30000,
  });

  const page = await browser.newPage();
  await page.setViewport({
    height: 900,
    width: 1366,
  });

  await page.goto('https://www.instagram.com/accounts/login/');
  await page.waitForSelector('input[name="username"]');
  await page.type('input[name=username]', '___your_id___');
  await page.type('input[name=password]', '___your_password___');
  await page.click('button[type=submit]');
  await page.waitForNavigation({ waitUntil: 'domcontentloaded' });

  await page.goto('https://www.instagram.com/direct/__your_frend_dm_direct_url__');

  await page.waitForSelector('textarea[placeholder="メッセージ..."]'); // japanese
  await page.type('textarea[placeholder="メッセージ..."]', "this is\nmultiple line");

  await browser.close();
};

enter image description here

I want to send a multi-line message in a single transmission.

Yuki
  • 1
  • 1
  • Different problem, but your click and `waitForNavigation` has a race condition and should be in a single `Promise.all()`. Or just skip that waitForNav since you're running a `goto` immediately afterwards. I don't have instagram otherwise I'd take a look, so if you're somehow able to reproduce this on a simple textarea, that'd be nice. Maybe try [How to add a new line in textarea element?](https://stackoverflow.com/questions/8627902/how-to-add-a-new-line-in-textarea-element) (a line feed as well as newline). – ggorlen Dec 16 '22 at 22:46
  • I ran some sample code and the newline worked fine on a basic textarea. So there must be something special about instagram's textarea here. – ggorlen Dec 17 '22 at 01:00
  • I found the reason. As I imagined, it was supposed to be sent by enter key. like this `if (e.keyCode == 13) {document.querySelector("#submit").click()}` . So when a line break was entered, the behavior was such that the text was sent. – Yuki Dec 18 '22 at 09:22
  • Nice--feel free to add a [self answer](https://stackoverflow.com/help/self-answer) – ggorlen Dec 18 '22 at 16:36

0 Answers0