// 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.
Asked
Active
Viewed 914 times
0

vsemozhebuty
- 12,992
- 1
- 26
- 26
-
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 23 '21 at 13:01
1 Answers
0
You need to catch the tab creation event:
const [url] = await Promise.all([
new Promise((resolve, reject) => {
browser.once('targetcreated', (target) => { resolve(target.url()); });
}),
page.click('#ctl00_chpMain_ucContractDetails'),
]);
console.log(url);

vsemozhebuty
- 12,992
- 1
- 26
- 26
-
Thankyou, you are a life saver.! If you can spare sometime and explain how did this work, what does browser.once do and what targetcreated means, that would be great. @vsemozhebuty – Muhammad Asad Chohan Sep 16 '21 at 18:52
-
@MuhammadAsadChohan You can see it in the docs in details: [event: 'targetcreated'](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#event-targetcreated), [eventEmitter.once(event, handler)](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#eventemitteronceevent-handler). Feel free to ask if something is unclear) – vsemozhebuty Sep 16 '21 at 18:57
-
@MuhammadAsadChohan Briefly: `browser.once()` is like `browser.addEventListener()` but listens for an event only once. The `'targetcreated'` event includes tab creation on a link click. – vsemozhebuty Sep 16 '21 at 19:04
-