playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Question] How to switch between tabs in same context?

Open sarut0bi opened this issue 6 years ago • 18 comments

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?

sarut0bi avatar Mar 20 '20 17:03 sarut0bi

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?

arjunattam avatar Mar 20 '20 17:03 arjunattam

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.

sarut0bi avatar Mar 20 '20 19:03 sarut0bi

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)?

arjunattam avatar Mar 20 '20 20:03 arjunattam

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)

sarut0bi avatar Mar 20 '20 21:03 sarut0bi

what if clicking on a link in page1 opens new tab? How to handle this situation?

Bharath-Kumar-S avatar Apr 29 '20 14:04 Bharath-Kumar-S

@Bharath-Kumar-S you can handle new tabs by listening to 'popup' event.

yury-s avatar Apr 29 '20 15:04 yury-s

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?

abhayp5 avatar May 18 '20 18:05 abhayp5

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.

Bharath-Kumar-S avatar May 19 '20 03:05 Bharath-Kumar-S

Both page1 and page2 will be active.

Bharath-Kumar-S avatar May 19 '20 03:05 Bharath-Kumar-S

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.

abhayp5 avatar May 19 '20 10:05 abhayp5

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 avatar May 25 '20 05:05 sublimator

@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.

abhayp5 avatar May 25 '20 18:05 abhayp5

Yes, need too!

sublimator avatar May 26 '20 01:05 sublimator

@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.

sublimator avatar May 27 '20 03:05 sublimator

@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

itsmano1993 avatar Sep 29 '20 12:09 itsmano1993

# 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

chasays avatar Apr 19 '21 13:04 chasays

@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?

venu44 avatar Dec 08 '21 13:12 venu44

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])

chandandanjo avatar Oct 19 '22 10:10 chandandanjo