Puppeteer使用page.type方法输入大段内容造成乱码

174次阅读
没有评论

一个项目运行得好好的,最近出现了点问题,这个问题的原因就是调用Puppeteer的page.type方法往输入框里输入文本时,空格不会输入导致的,比方说我需要往输入框里输入:Hello World,代码如下:

await page.type('input[placeholder="搜索我的文件"]', 'Hello World')

但是由于没有给我输入空格,导致输入框的内容是:HelloWorld,因此程序没有搜索到想要的数据。

解决方案:

使用DOM的API,document.execCommand,不过该API已经被废弃了,但是目前还是可以使用的。

await page.evaluate((originContent) => {
  document.execCommand('insertText', false, originContent)
}, originContent)

参考:

正文完
 0
wujingquan
版权声明:本站原创文章,由 wujingquan 于2023-11-21发表,共计441字。
转载说明:Unless otherwise specified, all articles are published by cc-4.0 protocol. Please indicate the source of reprint.
评论(没有评论)