Puppeteer:page.type() 遗漏了字符串的前几个字符 [英] Puppeteer: page.type() misses out first couple of characters of the string

查看:67
本文介绍了Puppeteer:page.type() 遗漏了字符串的前几个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 Puppeteer 的 page.type() 方法的问题.有时它可以完美地输入整个字符串,但有时它只是错过了前几个字符.

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.

我在 github 中发现了一个与此相关的问题,但已关闭.虽然我已经尝试了那里建议的一些解决方案,但它似乎不起作用.我也在 issue 中要求重新打开,但我想这需要一些时间.

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 寻求解决方案.代码如下:

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
});

我不知道这里出了什么问题.是不是因为解码需要一点时间并且 page.type() 在文本完全解码之前触发?或者找到帖子索引需要时间?我该如何解决这个问题?

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?

推荐答案

在您引用的问题页面上提到的一个建议对我也有用,那就是确保在输入之前将输入清零:

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');

这篇关于Puppeteer:page.type() 遗漏了字符串的前几个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆