Puppeteer:page.type() 遗漏了字符串的前几个字符
Puppeteer: page.type() misses out first couple of characters of the string
我遇到了 Puppeteer 的 page.type()
方法的问题。有时它会完美地键入整个字符串,但有时它只会漏掉前几个字符。
假设我有一个字符串:I may not be there yet but I am closer than yesterday
。它只是输入 y not be there yet but I am closer than yesterday
.
我在 github 中找到了一个与此相关的 issue,但它已被关闭。虽然我已经尝试了那里建议的一些解决方案,但它似乎不起作用。我也在这个问题中要求重新打开,但我想这需要一些时间。
于是想到向SO寻求解决方案。这是代码:
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() 在文本完全解码之前触发?或者找到 post 索引需要时间?我该如何解决这个问题?
在您引用的问题页面上提到的一个对我也有用的建议是确保在输入之前将输入清零:
await page.click('div[role="presentation"] span', {clickCount: 3});
await page.keyboard.press('Backspace');
我遇到了 Puppeteer 的 page.type()
方法的问题。有时它会完美地键入整个字符串,但有时它只会漏掉前几个字符。
假设我有一个字符串:I may not be there yet but I am closer than yesterday
。它只是输入 y not be there yet but I am closer than yesterday
.
我在 github 中找到了一个与此相关的 issue,但它已被关闭。虽然我已经尝试了那里建议的一些解决方案,但它似乎不起作用。我也在这个问题中要求重新打开,但我想这需要一些时间。
于是想到向SO寻求解决方案。这是代码:
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() 在文本完全解码之前触发?或者找到 post 索引需要时间?我该如何解决这个问题?
在您引用的问题页面上提到的一个对我也有用的建议是确保在输入之前将输入清零:
await page.click('div[role="presentation"] span', {clickCount: 3});
await page.keyboard.press('Backspace');