Getting the URL of a new tab in pyppeteer

1.3k Views Asked by At

I'm struggling to figure how to:

  1. Detect when a new tab is opened in chrome (e.g. from clicking something on a page)
  2. Get the url of that newly opened tab.

I have connected pyppeteer to my current chrome instance with this:

browser = await pyppeteer.connect(browserURL='http://127.0.0.1:9222')

And when I create a tab manually I have no problem getting the url.

url = 'https://www.google.com'
page = await browser.newPage()
await page.goto(url)
current_url = await page.evaluate('window.location.href', force_expr=True)

I have read that there is a way to do this in the original puppeteer with 'targetcreated':

browser.on('targetcreated', function(){
    console.log('New Tab Created');
})

So I'm asking how can I do something like:

// listen for new tab
newTab = browser.on('targetcreated') // This is where I'm stuck
// get url
current_url = await newTab.evaluate('window.location.href', force_expr=True)

I hope that all makes sense!

2

There are 2 best solutions below

0
On

You need to add a callback function like in your JavaScript example:

>>> from pyppeteer import launch
>>> def callback(target):
...     print(target.url)
... 
>>> browser = await launch()
>>> browser.on("targetcreated", callback)
<function callback at 0x7f408a767a60>
>>> page = await browser.newPage()
about:blank

The about:blank url was printed when a new blank tab was opened with browser.newPage().

Pyppeteer is using pyee.BaseEventEmitter.on under the hood.

0
On

1. Detect new tab

from pyppeteer import launch

browser = await launch({"headless": False})
page = await browser.newPage()
await page.goto("http://google.com")


len(await browser.pages()) # Should be 2 because default page and created page

page2 = await browser.newPage()
await page2.goto("http://yahoo.com")
len(await browser.pages()) # Should be 3 now

To detect new tab, check await browser.pages() And you can access every page:

pages = await browser.pages()

Which brings us to...

2. Get the url of that newly opened tab

for p in pages:
   print (p.url)

These are building blocks for you to figure out solution for your particular problem. Check how many pages you have and if there is a change, do something with a new page