简体   繁体   English

单击按钮后等待页面在新选项卡中加载

[英]Wait for the page to load in a new tab after clicking the button

I need to wait until the new page is loaded, which was opened in a new tab after clicking on the button.我需要等到加载新页面,单击按钮后在新选项卡中打开该页面。 That is, I click on a button, a new page opens (which should load) and on it I click another button.也就是说,我单击一个按钮,打开一个新页面(应该加载),然后单击另一个按钮。 I have some example code, but it doesn't work for some reason:我有一些示例代码,但由于某种原因它不起作用:

const page = await browser.newPage(); 
await page.goto('https://twitter.com/amazon/'); 
await page.click('.css-1dbjc4n:nth-child(1) > .css-1dbjc4n > .css-1dbjc4n > .css-901oao > .css-4rbku5',{waitUntil: ['load', 'domcontentloaded', 'networkidle0', 'networkidle2']}); 
const page2 = (await browser.pages())[2]; 
await page2.click('#nav-main > .nav-fill > #nav-xshop-container > #nav-xshop > .nav-a:nth-child(2)');

If I understand correctly, the problem is detecting when a new tab ("page") has opened and getting the new page object associated with the tab.如果我理解正确,问题是检测新选项卡(“页面”)何时打开并获取与该选项卡关联的新页面 object。

There are at least a couple techniques available.至少有几种可用的技术。 One method is promisifying the browser's "targetcreated" event as described here :一种方法是承诺浏览器的"targetcreated"事件如下所述:

const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch({headless: false});
  const [page] = await browser.pages(); 
  await page.goto("https://twitter.com/amazon");
  const amzSel = `.css-1dbjc4n:nth-child(1) > .css-1dbjc4n > 
                  .css-1dbjc4n > .css-901oao > .css-4rbku5`;
  await page.waitForSelector(amzSel, {visible: true});
  console.log((await browser.pages()).length); // => 1

  // method 1
  const newPagePromise = new Promise(resolve => 
    browser.once("targetcreated", target => resolve(target.page()))
  );
  await page.click(amzSel);
  const newPage = await newPagePromise;
  // --------

  console.log((await browser.pages()).length); // => 2
  await newPage.waitForSelector("#nav-link-prime", {visible: true});
  await newPage.click("#nav-link-prime");
  const sel = "#prime-header-CTA-announce";
  await newPage.waitForSelector(sel, {visible: true});
  console.log(await newPage.$eval(sel, el => el.innerText.trim())); // => TRY PRIME
  //await browser.close();
})();

Another approach is to use browser.waitForTarget to check when the target's opener() is the previous page target, as described here :另一种方法是使用browser.waitForTarget检查目标的opener()何时是上一页目标,如下所述:

const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch({headless: false});
  const [page] = await browser.pages(); 
  await page.goto("https://twitter.com/amazon");
  const amzSel = `.css-1dbjc4n:nth-child(1) > .css-1dbjc4n > 
                  .css-1dbjc4n > .css-901oao > .css-4rbku5`;
  await page.waitForSelector(amzSel, {visible: true});
  console.log((await browser.pages()).length); // => 1

  // method 2
  const pageTarget = page.target();
  await page.click(amzSel);
  const newTarget = await browser.waitForTarget(target =>
    target.opener() === pageTarget
  );
  const newPage = await newTarget.page();
  // --------

  console.log((await browser.pages()).length); // => 2
  await newPage.waitForSelector("#nav-link-prime", {visible: true});
  await newPage.click("#nav-link-prime");
  const sel = "#prime-header-CTA-announce";
  await newPage.waitForSelector(sel, {visible: true});
  console.log(await newPage.$eval(sel, el => el.innerText.trim())); // => TRY PRIME
  //await browser.close();
})();

As an aside, I'm not sure how important/significant this particular Twitter/Amazon example is, but #nav-xshop >.nav-a:nth-child(2) doesn't seem like a reliable selector (it appears to have a race condition between "Best Sellers" and "Prime")--I'd use #nav-link-prime since it's a direct id, if that's what you're looking for.顺便说一句,我不确定这个特定的 Twitter/Amazon 示例有多重要/重要,但#nav-xshop >.nav-a:nth-child(2)似乎不是一个可靠的选择器(它似乎在“Best Sellers”和“Prime”之间有一个竞争条件)——我会使用#nav-link-prime ,因为它是一个直接的id,如果你正在寻找的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM