Retrieve the page reference from ElementHandle.click()

Viewed 48

I am running a playwright script that opens a page (we'll call this page A) and then clicks a button that opens in a new tab. (page B)

page.navigate("https://<somethirdparty>.com"); //Page A
page.waitForLoadState(LoadState.NETWORKIDLE);

ElementHandle elementHandle = page.querySelector("#submit button[class='x']");
elementHandle.click(); //Opens in a new tab (Page B)

Now elementHandle.click() opens in a new tab but I have no access to the page object anymore. Any idea on how to access the page and wait for the page to be in a NETWORKIDLE state before I resume with my test?

1 Answers

You can use waitForPage.

Page newPage = context.waitForPage(() -> {
  elementHandle.click();
});
System.out.println(newPage.evaluate("location.href"));
Related