使用Puppeteer在新页面上运行JS

时间:2019-10-01 07:32:58

标签: chromium puppeteer

我以为我有这样一个工具非常简单的任务,但是我被卡住了。我只需要单击一个链接,然后在打开的页面上单击另一个链接即可。

(async () => {
    const browser = await puppeteer.launch({
        headless: false,
        devtools: true
    });

    const page = await browser.newPage();
    const uri = 'https://localhost/news';
    await page.goto(uri, {waitUntil: 'load', timeout: 30000});

    //save target of original page to know that this was the opener:
    const pageTarget = page.target();

    //execute click on first tab that triggers opening of new page
    await page.click('.menu a');
    //check that the first page opened this new page:
    const newTarget = await browser.waitForTarget(target => target.opener() === pageTarget);
    //get the new page object:
    const newPage = await newTarget.page();
    await page.waitFor(funcs.randd(5000, 8000));
    await newPage.click('.main-news a');
    await page.waitForNavigation();
});

此代码仅在菜单上第一次单击,但在新页面上没有任何反应。

我使用Linux Mint,nodejs 9.11.2。

0 个答案:

没有答案