0

使用 nodejs 和 puppeteer,它看起来像:

const puppeteer = require('puppeteer');              

const browser = await puppeteer.launch();             
const page1 = await browser.newPage();                
await page1.goto('https://google.com');                   

await page1.waitForSelector('#fsl > a:nth-child(4)'); 
const link = await page1.$('#fsl > a:nth-child(4)');  

const newPagePromise = new Promise(x => browser.once('targetcreated', target => x(target.page())));  // define promise
await link.click({button: 'middle'});                 
const page2 = await newPagePromise;                  
await page2.bringToFront();                           
  
...

await browser.close();                               

python和pyppeteer的等效代码字符串“定义承诺”是什么?

4

1 回答 1

2

我下面的代码可能会帮助你

result_page = asyncio.get_event_loop().create_future() # create new promise

# bind promise to watch event targetcreated, must before click to link
browser.once('targetcreated', lambda target: result_page.set_result(target))

await link.click({button: 'middle'});     # click link and open to other tab

page_in_new_tab = await (await result_page).page() # page in new tab here
于 2020-11-24T15:34:09.087 回答