With pyppeteer it is possible to get all open tabs via the .pages function. This is working fine until a website opens a new tab by itself (e.g. after a click on a button). In this case, the new tab isn’t listed in the return of **.pages*.
Is there a way to detect this new tab, so that I can work with it like I can do with the other tabs/pages?
(I didn't test it with puppeteer, but I think it'll behave the same.)
Code Example (Sadly I have to use Python 2.7., so I have to use yield from):
self.browser = yield from launch(appMode=True, closeAtExit=False)
pages = yield from self.browser.pages()
self.page = pages[len(pages) - 1] # Open w3schools in the init tab
yield from self.page.goto("https://www.w3schools.com/tags/att_a_target.asp")
link = yield from self.page.waitForSelector('a.w3-btn:nth-child(4)')
yield from link.click()
yield from asyncio.sleep(5) # Just to give some extra time...
pages1 = yield from self.browser.pages()
self.log.info("Count: " + str(len(pages1))) # Should be 2 now
for mpage in pages1:
self.log.info("URL: " + str(mpage.url))
Output:
TARGETS: {'246562630E35EEAD0384B80658C827F8': <pyppeteer.target.Target object at 0x03482F10>}
TARGETS: {'246562630E35EEAD0384B80658C827F8': <pyppeteer.target.Target object at 0x03482F10>}
INFO:__main__:Count: 1
INFO:__main__:URL: https://www.w3schools.com/tags/att_a_target.asp
INFO:__main__:Done!