Skip to content

Commit edb3238

Browse files
committed
Improve the Playwright integration.
1 parent 243db61 commit edb3238

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

computers/playwright/playwright.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import logging
1415
import termcolor
1516
import time
1617
import os
@@ -19,6 +20,7 @@
1920
Computer,
2021
EnvState,
2122
)
23+
import playwright.sync_api
2224
from playwright.sync_api import sync_playwright
2325
from typing import Literal
2426

@@ -85,6 +87,16 @@ def __init__(
8587
self._search_engine_url = search_engine_url
8688
self._highlight_mouse = highlight_mouse
8789

90+
def _handle_new_page(self, new_page: playwright.sync_api.Page):
91+
"""The Computer Use model only supports a single tab at the moment.
92+
93+
Some websites, however, try to open links in a new tab.
94+
For those situations, we intercept the page-opening behavior, and instead overwrite the current page.
95+
"""
96+
new_url = new_page.url
97+
new_page.close()
98+
self._page.goto(new_url)
99+
88100
def __enter__(self):
89101
print("Creating session...")
90102
self._playwright = sync_playwright().start()
@@ -101,6 +113,8 @@ def __enter__(self):
101113
self._page = self._context.new_page()
102114
self._page.goto(self._initial_url)
103115

116+
self._context.on("page", self._handle_new_page)
117+
104118
termcolor.cprint(
105119
f"Started local playwright.",
106120
color="green",
@@ -282,7 +296,9 @@ def current_state(self) -> EnvState:
282296
return EnvState(screenshot=screenshot_bytes, url=self._page.url)
283297

284298
def screen_size(self) -> tuple[int, int]:
285-
return self._screen_size
299+
viewport_size = self._page.viewport_size
300+
assert viewport_size is not None, "Failed to get viewport size."
301+
return viewport_size["width"], viewport_size["height"]
286302

287303
def highlight_mouse(self, x: int, y: int):
288304
if not self._highlight_mouse:

0 commit comments

Comments
 (0)