问题
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