|
6 | 6 | from playwright.sync_api import ( |
7 | 7 | Locator, |
8 | 8 | Page, |
9 | | - Playwright, |
10 | 9 | ) |
11 | 10 | from playwright.async_api import ( |
12 | 11 | Page as async_Page, |
13 | 12 | Locator as AsyncLocator, |
14 | | - Playwright as AsyncPlaywright, |
15 | 13 | BrowserContext as AsyncBrowserContext, |
16 | 14 | ) |
17 | 15 | from patchright.sync_api import sync_playwright |
@@ -82,24 +80,30 @@ def __init__(self, **kwargs: Unpack[StealthSession]): |
82 | 80 | def start(self): |
83 | 81 | """Create a browser for this instance and context.""" |
84 | 82 | if not self.playwright: |
85 | | - self.playwright: Playwright = sync_playwright().start() # pyright: ignore [reportAttributeAccessIssue] |
| 83 | + self.playwright = sync_playwright().start() |
86 | 84 |
|
87 | | - if self._config.cdp_url: # pragma: no cover |
88 | | - browser = self.playwright.chromium.connect_over_cdp(endpoint_url=self._config.cdp_url) |
89 | | - self.context = browser.new_context(**self._context_options) |
90 | | - else: |
91 | | - self.context = self.playwright.chromium.launch_persistent_context(**self._launch_options) |
| 85 | + try: |
| 86 | + if self._config.cdp_url: # pragma: no cover |
| 87 | + browser = self.playwright.chromium.connect_over_cdp(endpoint_url=self._config.cdp_url) |
| 88 | + self.context = browser.new_context(**self._context_options) |
| 89 | + else: |
| 90 | + self.context = self.playwright.chromium.launch_persistent_context(**self._launch_options) |
92 | 91 |
|
93 | | - for script in _compiled_stealth_scripts(): |
94 | | - self.context.add_init_script(script=script) |
| 92 | + for script in _compiled_stealth_scripts(): |
| 93 | + self.context.add_init_script(script=script) |
95 | 94 |
|
96 | | - if self._config.init_script: # pragma: no cover |
97 | | - self.context.add_init_script(path=self._config.init_script) |
| 95 | + if self._config.init_script: # pragma: no cover |
| 96 | + self.context.add_init_script(path=self._config.init_script) |
98 | 97 |
|
99 | | - if self._config.cookies: # pragma: no cover |
100 | | - self.context.add_cookies(self._config.cookies) |
| 98 | + if self._config.cookies: # pragma: no cover |
| 99 | + self.context.add_cookies(self._config.cookies) |
101 | 100 |
|
102 | | - self._is_alive = True |
| 101 | + self._is_alive = True |
| 102 | + except Exception: |
| 103 | + # Clean up playwright if browser setup fails |
| 104 | + self.playwright.stop() |
| 105 | + self.playwright = None |
| 106 | + raise |
103 | 107 | else: |
104 | 108 | raise RuntimeError("Session has been already started") |
105 | 109 |
|
@@ -308,26 +312,31 @@ def __init__(self, **kwargs: Unpack[StealthSession]): |
308 | 312 | async def start(self): |
309 | 313 | """Create a browser for this instance and context.""" |
310 | 314 | if not self.playwright: |
311 | | - self.playwright: AsyncPlaywright = await async_playwright().start() # pyright: ignore [reportAttributeAccessIssue] |
312 | | - |
313 | | - if self._config.cdp_url: |
314 | | - browser = await self.playwright.chromium.connect_over_cdp(endpoint_url=self._config.cdp_url) |
315 | | - self.context: AsyncBrowserContext = await browser.new_context(**self._context_options) |
316 | | - else: |
317 | | - self.context: AsyncBrowserContext = await self.playwright.chromium.launch_persistent_context( |
318 | | - **self._launch_options |
319 | | - ) |
| 315 | + self.playwright = await async_playwright().start() |
| 316 | + try: |
| 317 | + if self._config.cdp_url: |
| 318 | + browser = await self.playwright.chromium.connect_over_cdp(endpoint_url=self._config.cdp_url) |
| 319 | + self.context: AsyncBrowserContext = await browser.new_context(**self._context_options) |
| 320 | + else: |
| 321 | + self.context: AsyncBrowserContext = await self.playwright.chromium.launch_persistent_context( |
| 322 | + **self._launch_options |
| 323 | + ) |
320 | 324 |
|
321 | | - for script in _compiled_stealth_scripts(): |
322 | | - await self.context.add_init_script(script=script) |
| 325 | + for script in _compiled_stealth_scripts(): |
| 326 | + await self.context.add_init_script(script=script) |
323 | 327 |
|
324 | | - if self._config.init_script: # pragma: no cover |
325 | | - await self.context.add_init_script(path=self._config.init_script) |
| 328 | + if self._config.init_script: # pragma: no cover |
| 329 | + await self.context.add_init_script(path=self._config.init_script) |
326 | 330 |
|
327 | | - if self._config.cookies: |
328 | | - await self.context.add_cookies(self._config.cookies) # pyright: ignore |
| 331 | + if self._config.cookies: |
| 332 | + await self.context.add_cookies(self._config.cookies) # pyright: ignore |
329 | 333 |
|
330 | | - self._is_alive = True |
| 334 | + self._is_alive = True |
| 335 | + except Exception: |
| 336 | + # Clean up playwright if browser setup fails |
| 337 | + await self.playwright.stop() |
| 338 | + self.playwright = None |
| 339 | + raise |
331 | 340 | else: |
332 | 341 | raise RuntimeError("Session has been already started") |
333 | 342 |
|
|
0 commit comments