mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-08 22:15:08 +00:00
Added ability to pass arguments to the Playwright browser (#13146)
- **Description:** Enhanced `create_sync_playwright_browser` and `create_async_playwright_browser` functions to accept a list of arguments. These arguments are now forwarded to `browser.chromium.launch()` for customizable browser instantiation. - **Issue:** #13143 - **Dependencies:** None - **Tag maintainer:** @eyurtsev, - **Twitter handle:** Dr_Bearden --------- Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
This commit is contained in:
parent
dcccf8fa66
commit
77a15fa988
@ -2,7 +2,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
from typing import TYPE_CHECKING, Any, Coroutine, TypeVar
|
from typing import TYPE_CHECKING, Any, Coroutine, List, Optional, TypeVar
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from playwright.async_api import Browser as AsyncBrowser
|
from playwright.async_api import Browser as AsyncBrowser
|
||||||
@ -50,12 +50,15 @@ def get_current_page(browser: SyncBrowser) -> SyncPage:
|
|||||||
return context.pages[-1]
|
return context.pages[-1]
|
||||||
|
|
||||||
|
|
||||||
def create_async_playwright_browser(headless: bool = True) -> AsyncBrowser:
|
def create_async_playwright_browser(
|
||||||
|
headless: bool = True, args: Optional[List[str]] = None
|
||||||
|
) -> AsyncBrowser:
|
||||||
"""
|
"""
|
||||||
Create an async playwright browser.
|
Create an async playwright browser.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
headless: Whether to run the browser in headless mode. Defaults to True.
|
headless: Whether to run the browser in headless mode. Defaults to True.
|
||||||
|
args: arguments to pass to browser.chromium.launch
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
AsyncBrowser: The playwright browser.
|
AsyncBrowser: The playwright browser.
|
||||||
@ -63,15 +66,18 @@ def create_async_playwright_browser(headless: bool = True) -> AsyncBrowser:
|
|||||||
from playwright.async_api import async_playwright
|
from playwright.async_api import async_playwright
|
||||||
|
|
||||||
browser = run_async(async_playwright().start())
|
browser = run_async(async_playwright().start())
|
||||||
return run_async(browser.chromium.launch(headless=headless))
|
return run_async(browser.chromium.launch(headless=headless, args=args))
|
||||||
|
|
||||||
|
|
||||||
def create_sync_playwright_browser(headless: bool = True) -> SyncBrowser:
|
def create_sync_playwright_browser(
|
||||||
|
headless: bool = True, args: Optional[List[str]] = None
|
||||||
|
) -> SyncBrowser:
|
||||||
"""
|
"""
|
||||||
Create a playwright browser.
|
Create a playwright browser.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
headless: Whether to run the browser in headless mode. Defaults to True.
|
headless: Whether to run the browser in headless mode. Defaults to True.
|
||||||
|
args: arguments to pass to browser.chromium.launch
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
SyncBrowser: The playwright browser.
|
SyncBrowser: The playwright browser.
|
||||||
@ -79,7 +85,7 @@ def create_sync_playwright_browser(headless: bool = True) -> SyncBrowser:
|
|||||||
from playwright.sync_api import sync_playwright
|
from playwright.sync_api import sync_playwright
|
||||||
|
|
||||||
browser = sync_playwright().start()
|
browser = sync_playwright().start()
|
||||||
return browser.chromium.launch(headless=headless)
|
return browser.chromium.launch(headless=headless, args=args)
|
||||||
|
|
||||||
|
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
|
Loading…
Reference in New Issue
Block a user