Puppeteer - New window about:blank

痴心易碎 提交于 2019-12-12 23:31:20

问题


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?

Working (Link with something):

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

回答1:


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

the code follows to these functions:

_targetCreated
_targetInfoChanged
_targetInfoChanged

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

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

this is the flow:

_targetCreated
_targetInfoChanged

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.



来源:https://stackoverflow.com/questions/51390268/puppeteer-new-window-aboutblank

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!