Puppeteer - 新窗口关于:空白 [英] Puppeteer - New window about:blank

查看:73
本文介绍了Puppeteer - 新窗口关于:空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与点击链接时从网站弹出的新窗口进行交互.当这个新窗口打开时,链接是:about:blank.当链接有除空白之外的其他内容时,它会起作用.有人知道解决方法吗?

Am trying to interact with a new window that pops from a website when hittin a link. When this new window opens the link is: about:blank. When the link have something other than blank it works. Does anybody knows an workaround?

工作(与某事链接):

const newPagePromise = new Promise(x => browser.once('targetcreated', target => x(target.page())));
await page.click('#link-of-new-window');
const newPage = await newPagePromise;

不工作(关于:空白):

Not Working (about:blank):

//not entering
browser.on('targetcreated', function(){
    console.log('New Tab Created');
});

const newPagePromise = new Promise(x => browser.once('targetcreated', target => x(target.page())));
await page.click('#link-of-new-window');            
const newPage = await newPagePromise; //hang in here forever

推荐答案

显然这是一个错误.如前所述,它在此处报告:Puppeteer 的 github.

Apparently this is a bug. As mentioned before it's reported here: Puppeteer's github.

作为一种解决方法,我注意到当创建一个带有链接的新窗口时,如下所示:

As a workaround i noticied that when a new window is created with some link, like this:

await page.evaluate(async () => await window.open("http://google.com","Schedule","menubar=0,scrollbars=1,resizable=1"));

这些函数的代码如下:

_targetCreated
_targetInfoChanged
_targetInfoChanged

但是,当使用 auto:blank 创建新窗口时

However, when a new window is created with auto:blank

await page.evaluate(async () => await window.open("","Schedule","menubar=0,scrollbars=1,resizable=1"));

这是流程:

_targetCreated
_targetInfoChanged

因此,当我单击我知道是 auto:blank 的链接时,我会搜索未初始化的目标target._isInitialized"并再次调用 _targetInfoChanged,将 targetInfo 作为参数传递.它现在正在工作.在修复错误之前,这只是一种解决方法.

So when i click a link that i know is auto:blank i search for a target that is not initialized 'target._isInitialized' and call the _targetInfoChanged again passing the targetInfo as parameter. It's working for now. This is just an workaround until the bug is fixed.

这篇关于Puppeteer - 新窗口关于:空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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