|
| 1 | +from browser_use import BrowserSession |
| 2 | + |
| 3 | +# Define a subclass of BrowserSession that overrides _setup_viewports (which mishandles resizeing on connecting via cdp) |
| 4 | +class BrowserSessionCustomResize(BrowserSession): |
| 5 | + async def _setup_viewports(self) -> None: |
| 6 | + """Resize any existing page viewports to match the configured size, set up storage_state, permissions, geolocation, etc.""" |
| 7 | + |
| 8 | + assert self.browser_context, 'BrowserSession.browser_context must already be set up before calling _setup_viewports()' |
| 9 | + |
| 10 | + self.browser_profile.window_size = {"width": 1024, "height": 786} |
| 11 | + self.browser_profile.viewport = {"width": 1024, "height": 786} |
| 12 | + self.browser_profile.screen = {"width": 1024, "height": 786} |
| 13 | + self.browser_profile.device_scale_factor = 1.0 |
| 14 | + |
| 15 | + # log the viewport settings to terminal |
| 16 | + viewport = self.browser_profile.viewport |
| 17 | + # if we have any viewport settings in the profile, make sure to apply them to the entire browser_context as defaults |
| 18 | + if self.browser_profile.permissions: |
| 19 | + try: |
| 20 | + await self.browser_context.grant_permissions(self.browser_profile.permissions) |
| 21 | + except Exception as e: |
| 22 | + print(e) |
| 23 | + try: |
| 24 | + if self.browser_profile.default_timeout: |
| 25 | + self.browser_context.set_default_timeout(self.browser_profile.default_timeout) |
| 26 | + if self.browser_profile.default_navigation_timeout: |
| 27 | + self.browser_context.set_default_navigation_timeout(self.browser_profile.default_navigation_timeout) |
| 28 | + except Exception as e: |
| 29 | + print(e) |
| 30 | + try: |
| 31 | + if self.browser_profile.extra_http_headers: |
| 32 | + self.browser_context.set_extra_http_headers(self.browser_profile.extra_http_headers) |
| 33 | + except Exception as e: |
| 34 | + print(e) |
| 35 | + |
| 36 | + try: |
| 37 | + if self.browser_profile.geolocation: |
| 38 | + await self.browser_context.set_geolocation(self.browser_profile.geolocation) |
| 39 | + except Exception as e: |
| 40 | + print(e) |
| 41 | + |
| 42 | + await self.load_storage_state() |
| 43 | + |
| 44 | + page = None |
| 45 | + |
| 46 | + for page in self.browser_context.pages: |
| 47 | + # apply viewport size settings to any existing pages |
| 48 | + if viewport: |
| 49 | + await page.set_viewport_size(viewport) |
| 50 | + |
| 51 | + # show browser-use dvd screensaver-style bouncing loading animation on any about:blank pages |
| 52 | + if page.url == 'about:blank': |
| 53 | + await self._show_dvd_screensaver_loading_animation(page) |
| 54 | + |
| 55 | + page = page or (await self.browser_context.new_page()) |
| 56 | + |
| 57 | + if (not viewport) and (self.browser_profile.window_size is not None) and not self.browser_profile.headless: |
| 58 | + # attempt to resize the actual browser window |
| 59 | + |
| 60 | + # cdp api: https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-setWindowBounds |
| 61 | + try: |
| 62 | + cdp_session = await page.context.new_cdp_session(page) |
| 63 | + window_id_result = await cdp_session.send('Browser.getWindowForTarget') |
| 64 | + await cdp_session.send( |
| 65 | + 'Browser.setWindowBounds', |
| 66 | + { |
| 67 | + 'windowId': window_id_result['windowId'], |
| 68 | + 'bounds': { |
| 69 | + **self.browser_profile.window_size, |
| 70 | + 'windowState': 'normal', # Ensure window is not minimized/maximized |
| 71 | + }, |
| 72 | + }, |
| 73 | + ) |
| 74 | + await cdp_session.detach() |
| 75 | + except Exception as e: |
| 76 | + _log_size = lambda size: f'{size["width"]}x{size["height"]}px' |
| 77 | + try: |
| 78 | + # fallback to javascript resize if cdp setWindowBounds fails |
| 79 | + await page.evaluate( |
| 80 | + """(width, height) => {window.resizeTo(width, height)}""", |
| 81 | + **self.browser_profile.window_size, |
| 82 | + ) |
| 83 | + return |
| 84 | + except Exception as e: |
| 85 | + pass |
0 commit comments