1

I want to create a method to Click on given element and open it in new tab. I manage to open new tab on click:

 public async Task ClickMenuItemNewTab(string menuItem, string section, string header)
{
    var context = BrowserSession.Browser.Contexts[0];
    var newPage = await context.RunAndWaitForPageAsync(async () =>
    {
        await _homePage.ClickMenuItem(menuItem, section, header, new() { Button = MouseButton.Middle });
    });

    await newPage.WaitForLoadStateAsync();

}

but I don't know how can I switch to page in new tab. Do I have to create new browser context for it. Or is it way to switch to the page in same context. Thank you for any help.

laxsore
  • 143
  • 1
  • 2
  • 9

1 Answers1

0
await Page.BringToFrontAsync();

https://playwright.dev/dotnet/docs/api/class-page#page-bring-to-front

BMar cg
  • 13
  • 4
  • Thank you very much for help. I can see that page is moved to the front, but still I cannot interact with it (ex. click on element) – laxsore Jan 25 '23 at 07:20
  • @laxsore Since it is a new page (even within the same context), any locator method has to be done on that new page as well to use it there. Are you locating that element you want to click on, for example, on the new page or the old/first one? – David R Feb 12 '23 at 08:24
  • I have similar issue in Java. So I used bringToFront method, then page.locator(String selector) and cannot interact with it. How to handle with this? – goney225 Jul 27 '23 at 09:40
  • Did you use the new page to call the locator method? – David R Jul 27 '23 at 20:01
  • No, in my scenario I have two browsers and bringToFront(); redirects to the first browser. On this first browser I am trying to click on button element but cannot interact. – goney225 Jul 28 '23 at 06:59
  • Is the button element located by the page belonging to that browser? Is it [actionable](https://playwright.dev/java/docs/actionability)? Also to note, Playwright doesn’t actually care if you use bringToFront, as all pages are always active and usable. That’s mainly there because it was requested, perhaps for if the app itself cares about visibility state or something. Playwright just cares which page you tell it to interact with or locate within. – David R Jul 29 '23 at 00:13