Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sarut0bi opened this issue Mar 20, 2020 · 18 comments
Closed

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

sarut0bi opened this issue Mar 20, 2020 · 18 comments

Comments

@sarut0bi
Copy link

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?

@arjunattam
Copy link
Contributor

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?

@sarut0bi
Copy link
Author

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.

@arjunattam
Copy link
Contributor

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

@sarut0bi
Copy link
Author

sarut0bi commented Mar 20, 2020

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)

@Bharath-Kumar-S
Copy link

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

@yury-s
Copy link
Member

yury-s commented Apr 29, 2020

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

@abhayp5
Copy link

abhayp5 commented May 18, 2020

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?

@Bharath-Kumar-S
Copy link

Bharath-Kumar-S commented May 19, 2020

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
Copy link

Both page1 and page2 will be active.

@abhayp5
Copy link

abhayp5 commented May 19, 2020

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.

@sublimator
Copy link

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

@abhayp5
Copy link

abhayp5 commented May 25, 2020

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

@sublimator
Copy link

Yes, need too!

@sublimator
Copy link

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

@itsmano1993
Copy link

@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

@chasays
Copy link

chasays commented Apr 19, 2021

# 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

@venu44
Copy link

venu44 commented Dec 8, 2021

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

@chandandanjo
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants