[Question] How to switch between tabs in same context?
Currently i am unable to switch between tabs i explain:
- i open new browser then new Context (context1)
- i open new page (page1) on context1
- i open new page (page2) still on context1
- i see my 2 opened page in the context but i am unable to switch between them :(
=> Is there a way to do context1.setActivePage(page1) ?
I should also create another context but if each page need his own context what is the gain of context?
Hi @sarut0bi, pages in Playwright behave as if they are active and visible throughout execution, and do not require manual switching. Can you elaborate more on why you would like to switch between tabs?
if i have to interract from a backoffice front for example:
- i am on product page (page 1) > add to cart button is enabled
- i set stock to 0 from backoffice (page 2)
- i refresh page 1 > add to cart button is enabled.
Thanks. I'm not entirely sure I follow. It would be possible to reload the page 1. How would setActivePage particularly help? Are you running the execution headfully? Is there manual intervention involved (not from JS code)?
i thought i can't interract on page1 once page2 is opened in fact: if i do :
let browser = await chromium.launch({ headless: headless });
let context = await browser.newContext();
let page1 = await context.newPage();
await page1.goto('http://www.google.com);
let page2 = await context.newPage();
await page2.goto('http://www.ebay.com);
await page1.fill('[name="q"]', 'plop');
=> it works as it should :) (sorry for mistake)
what if clicking on a link in page1 opens new tab? How to handle this situation?
@Bharath-Kumar-S you can handle new tabs by listening to 'popup' event.
If 2 tabs of same context are opened by
let context = await browser.newContext(); let page1 = await context.newPage(); await page1.goto('http://www.google.com); let page2 = await context.newPage(); await page2.goto('http://www.ebay.com);
How to move focus back to page1?
You can use page1 variable to access google.com tab and page2 to access ebay.com tab. You don't have to switch it like how we do it in selenium or protractor.
Both page1 and page2 will be active.
My product is based on the visibility, what an end user is viewing. So despite of having control of elements in all pages I need to switch between the tabs or pages. So if we have some thing to aching it using playwright please share.
Yes, what is the document.visibilityState of each Page ?
What if you have document.addEventListener('visibilitychange', listener) ?
e.g. an extension that performs actions based on the "active" tab
@sublimator thanks for suggestion but you completely missed it. I am asking the functionality to change the tab or switch to previous tab in playwright. I am not trying to implement any event listener functionality. In simple words ability to change or switch the browser tab using playwright if we can.
Yes, need too!
@abhayp5
@sublimator thanks for suggestion but you completely missed it.
Sorry, I was actually asking what the visibility state of each Page is supposed be if you simply use the Page handles as if they had focus. What if your page does something based on the visibility state?
I want to test an extension and in puppeteer we can use page.bringToFront() to focus a given tab.
@Bharath-Kumar-S you can handle new tabs by listening to
'popup'event. Hi Can you please give code snippet for python on this case? Thanks Mano
# Get page after a specific action (e.g. clicking a link)
with context.expect_page() as new_page_info:
page.click('a[target="_blank"]') # Opens a new tab
new_page = new_page_info.value
new_page.wait_for_load_state()
print(new_page.title())
please refer to this link.https://playwright.dev/python/docs/multi-pages?_highlight=multi#handling-new-pages
@Bharath-Kumar-S you can handle new tabs by listening to
'popup'event. Hi Can you please give code snippet for python on this case? Thanks Mano
@Bharath-Kumar-S Can you please let me know if you are able to handle multiple tabs in playwright and switch between them?
Assume you only created one page (via browser context), but for some reason, new pages/tabs open.
You can have a list of all the pages by : context.pages,
Now each element of that list represents a <class 'playwright.async_api._generated.Page'> object.
So, now you can assign each page to any variable and access it. (For eg. page2 = context.pages[1])