如何获取在不同选项卡中使用 puppeteer 打开的页面的 url,或从“单击此处”link 中提取 URL?
how can I get the url of a page that opened in a different tab with puppeteer, or extract the URL from the " click here " link?
// this clicks on the link after page is loaded,
// there is whole process going before coming to this point.
await page.click('#ctl00_chpMain_ucContractDetails');
const url = await newPage.evaluate(() => document.location.href);
console.log(url); // this code didnt work for me.
您需要捕捉选项卡创建事件:
const [url] = await Promise.all([
new Promise((resolve, reject) => {
browser.once('targetcreated', (target) => { resolve(target.url()); });
}),
page.click('#ctl00_chpMain_ucContractDetails'),
]);
console.log(url);
// this clicks on the link after page is loaded,
// there is whole process going before coming to this point.
await page.click('#ctl00_chpMain_ucContractDetails');
const url = await newPage.evaluate(() => document.location.href);
console.log(url); // this code didnt work for me.
您需要捕捉选项卡创建事件:
const [url] = await Promise.all([
new Promise((resolve, reject) => {
browser.once('targetcreated', (target) => { resolve(target.url()); });
}),
page.click('#ctl00_chpMain_ucContractDetails'),
]);
console.log(url);