s.khan
s.khan

Reputation: 395

Puppeteer: page.type() misses out first couple of characters of the string

I am facing an issue with Puppeteer's page.type() method. Sometimes it types a whole string perfectly but at times it just misses out the first couple of characters.

Suppose I have a string: I may not be there yet but I am closer than yesterday. It just types y not be there yet but I am closer than yesterday.

I have found an issue in github related to this but it has been closed. Although I have tried some of the solutions suggested there but it doesn't seem to work. I have also asked in the issue to be reopened but I guess it will take some time.

So thought of seeking a solution from SO. Here's the code:

const blog = {
     postContent: [
          {
               text: "SSBtYXkgbm90IGJlIHRoZXJlIHlldCwgYnV0IEknbSBjbG9zZXIgdGhhbiB5ZXN0ZXJkYXk=",
               status: "pending"
          },
          {
               text: "RG9uJ3QgcXVpdGUuIFN1ZmZlciBub3cgYW5kIGxpdmUgdGhlIHJlc3Qgb2YgeW91ciBsaWZlIGFzIGEgY2hhbXBpb24u",
               status: "pending"
          },
          {
               text: "TGlmZSBpcyBub3QgYWJvdXQgd2lubmluZy4gSXQncyBhYm91dCBub3QgZ2l2aW5nIHVwIQ==",
               status: "pending"
          }
     ]
};


await page.waitForSelector('div[role="presentation"]' );
await page.focus('div[role="presentation"]');

// finding the index of the blog post which is pending
const pendingPostIndex = await blog.postContent.findIndex( item => item.status === "pending" );

// decoding post content
const textContent = `${Buffer.from(blog.postContent[pendingPostIndex].text, 'base64').toString()}`;

// this gets the full text without missing characters
console.log('text content of pending post: ', textContent);

// first couple characters are missing most of the time
await page.type('div[role="presentation"] span', textContent, {
         delay: 100
});

I don't know what could be going wrong here. Is it because decoding takes a little bit of time and page.type() fires before the text is completely decoded? Or finding the post index takes time? How can I fix this?

Upvotes: 2

Views: 1516

Answers (1)

Benny
Benny

Reputation: 166

A suggestion mentioned on the issue page you cited, and that has also worked for me is to be sure the input is zeroed out before typing:

await page.click('div[role="presentation"] span', {clickCount: 3});
await page.keyboard.press('Backspace');

Upvotes: 2

Related Questions