Skip to content

Commit 902e651

Browse files
authored
Merge pull request #11 from kirill-markin/feature/improve-local-page-handling
Add local page handling to LocalPlaywrightComputer
2 parents 7a0d119 + 671e22a commit 902e651

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

computers/local_playwright.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,32 @@ def _get_browser_and_page(self) -> tuple[Browser, Page]:
1818
args=launch_args,
1919
env={"DISPLAY": ":0"}
2020
)
21-
page = browser.new_page()
21+
22+
context = browser.new_context()
23+
24+
# Add event listeners for page creation and closure
25+
context.on("page", self._handle_new_page)
26+
27+
page = context.new_page()
2228
page.set_viewport_size({"width": width, "height": height})
29+
page.on("close", self._handle_page_close)
30+
2331
page.goto("https://bing.com")
32+
2433
return browser, page
34+
35+
def _handle_new_page(self, page: Page):
36+
"""Handle the creation of a new page."""
37+
print("New page created")
38+
self._page = page
39+
page.on("close", self._handle_page_close)
40+
41+
def _handle_page_close(self, page: Page):
42+
"""Handle the closure of a page."""
43+
print("Page closed")
44+
if self._page == page:
45+
if self._browser.contexts[0].pages:
46+
self._page = self._browser.contexts[0].pages[-1]
47+
else:
48+
print("Warning: All pages have been closed.")
49+
self._page = None

0 commit comments

Comments
 (0)